How to get an array of images?

Akshat picture Akshat · Jun 27, 2013 · Viewed 12.3k times · Source

I want to make a Battery app which should notify the battery percentage to user in form of image. Now for battery percentage 0-100, I need 101 images in all. What I want to do is that is it possible to insert all these images in an array and use them accordingly. For instance, if my battery percentage is 47%, the image in array at position 47 should be the resource. That is, suppose I have ImageView named Battery, and I have an array named batteryimage(containing all the 101 images) then I should be able to do this,

Battery.setBackgroundResource(R.drawable.batteryimage[p])

where p will be the current percentage of battery, which I will get by code.

Hope you all understood what I want.. Thanx in advance..

Answer

stinepike picture stinepike · Jun 27, 2013

define a array of image id like this

int[] p = {R.drawable.image1, R.drawable.image2....}

now for different condition use member of this array like

Battery.setBackgroundResource(p[0])  // or p[1]

or you can use ENUM to make it more readable..

But.. I think you should think in another way. instead of using 100 different image create a custom progressbar to show different state of your battery.

To know the basics of Progress bar see doc here and to create custom progressbar you can check this so question