tejas tejas - 11 months ago 36
Android Question

Changing image view background dynamically

I have a a set of 10 imageviews in my layout. I have given them sequential id's also as



Now I want to change background dynamically.

int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {

Now inside the for loop I want to set the image view background dynamically. i,e if the currentpoi value is 3, background of 3 image views should be changed. What ever the times the for loop iterates that many image view's background should be changed. Hope the question is clear now.

Note : I have only 1 image progressgreen that need to be set to 10 image views


Finally I did this in the following way,

I placed all the id's in the array as

int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};


int pindex = 0;

for (pindex; pindex <currentPoi; pindex++) {

    ImageView img = (ImageView) findViewById(imageViews[pindex]) ;

Now, I am able to change the images dynamically.

@goto10. Thanks for your help. I will debug your point to see what went wrong in my side