Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array of images?

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..

like image 420
Akshat Avatar asked Jun 27 '13 11:06

Akshat


1 Answers

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

like image 86
stinepike Avatar answered Sep 25 '22 13:09

stinepike