4

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

android:id="@+id/pb1"

android:id="@+id/pb2"

Now I want to change background dynamically.

    int totalPoi = listOfPOI.size();
    int currentPoi = (j/totalPoi)*10;
    for (i=1;i<=currentPoi;i++) {
          imageview.setBackgroundResource(R.drawable.progressgreen);
}

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

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
tejas
  • 2,435
  • 10
  • 37
  • 54

6 Answers6

7

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};

Now:

int pindex = 0;

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

    ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
    img.setImageResource(R.drawable.progressgreen);
}

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

tejas
  • 2,435
  • 10
  • 37
  • 54
3

Create an ImageView array:

ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);

Now iterate the loop to set the background of images like this:

for (i=1;i<=currentPoi;i++) 
{
    views[i-1].setBackgroundResource(R.drawable.progressgreen);
}
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
2

you can do this by setting the name of drawables something like: img_1, img_2, img_3...

for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable",  getPackageName()));
}
Permita
  • 5,503
  • 1
  • 16
  • 21
  • That doesn't set the background on multiple ImageViews. It sets the background on the same ImageView multiple times. – goto10 Oct 24 '11 at 06:17
1

You'll need to give your ImageViews sequential ids, such as "@+id/pb1" and "@+id/pb2", etc.. Then you can get each of them in the loop like this:

for (i=1;i<=currentPoi;i++) {
    // Find the image view based on it's name. We know it's pbx where 'x' is a number
    // so we concatenate "pb" with the value of i in our loop to get the name
    // of the identifier we're looking for. getResources.getIdentifier() is able to use
    // this string value to find the ID of the imageView
    int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");

    // Use the ID retrieved in the previous line to look up the ImageView object
    ImageView imageView = (ImageView) findViewById(imageViewId);

    // Set the background for the ImageView
    imageView.setBackgroundResource(R.drawable.progressgreen);
}

Replace com.your.package.name with your application's package.

goto10
  • 4,370
  • 1
  • 22
  • 33
  • can you tell me what would be imageview in imageview.setBackgroundResource(R.drawable.progressgreen);? – tejas Oct 24 '11 at 06:40
  • imageView is set in the previous line which looks up the ImageView based on the ID. The ID is calculated from the name of the ImageView. I'll add some comments to the code to make it a little more clear. – goto10 Oct 24 '11 at 06:43
  • yes got it, but have one silly doubt, pb which you are referring to, is the image view object right? – tejas Oct 24 '11 at 06:57
  • Yes. pb1, pb2, etc. are ImageViews. – goto10 Oct 24 '11 at 06:59
  • It is throwing me null in the line ImageView imageView = (ImageView) findViewByid(imageViewId);, for imageView though in the previous line imageViewId is not null – tejas Oct 24 '11 at 07:00
  • That is strange. Does imageViewId value match any of the ID values in you R.java file? – goto10 Oct 24 '11 at 07:27
  • No, at first only I am getting imageViewId as 0, I think it is supposed to be @+id/pb1 or pb1. – tejas Oct 24 '11 at 08:38
  • Make sure the package name is correct. It should match the package attribute in your manifest. – goto10 Oct 24 '11 at 08:41
  • Yes, it is matching. What it should return for imageViewId, 0 or pb1? – tejas Oct 24 '11 at 08:44
  • imageViewId should have the same value as R.id.pb1 (2, 3, etc.) from your R.java file. It'll be a big number. – goto10 Oct 24 '11 at 08:47
  • I get 0, for int imageViewId = getResources().getIdentifier("pb" + pindex, "id", "com.racontrs"); where com.racontrs is the package name given in the manifest file.(pindex is nothing but i) – tejas Oct 24 '11 at 08:51
  • Do you have a line in your R.java that looks like this: `public static final int pb1=0x7f080024;` (the number will be different) If not then your ids aren't right for the ImageViews in your layout. – goto10 Oct 24 '11 at 08:54
  • Yes, I do have that line..public static final int pb1=0x7f09002c; – tejas Oct 24 '11 at 08:57
  • I'm running out of ideas. Is that line inside this static class: `public static final class id {` ? What is the value of pindex when imageViewId is 0? – goto10 Oct 24 '11 at 09:00
  • Yes, it is inside that class. pindex value is 0 – tejas Oct 24 '11 at 09:02
  • Do you have a pb0? The example in your question started with i=1. Make sure i starts at 1 if your first imageView is pb1. – goto10 Oct 24 '11 at 09:05
  • No, i don't have a pb0. What I am trying is say there are 40 poi's then as I move on to the next poi's means 4,5,6 etc. The image view background keeps changing. Here I have only 10 image views. so I am doing int totalPoi = listOfPOI.size(); and then int currentPoi = (int) (((float)(j * 1.0)/totalPoi)*10); This should give me the current value in the list. Now till this value , the image view should change. that means if the currentPoi is 5, 4 out of 10 image view should have differnt image – tejas Oct 24 '11 at 09:25
  • As I said you just need to make sure that i will only have values that make sense for your images. If your first image is pb1 then start i at 1. I would think that `for (int i = 1; i < currentPoi; i++)' should do it, but you'll need to make sure that works for your data. The code I provided will work for values of i that correspond to images with the appropriate id. – goto10 Oct 24 '11 at 09:37
  • 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}; Now, int pindex = 0; for (pindex; pindex – tejas Oct 24 '11 at 10:04
1

Try this code..... Create image Array..

private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
        R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };

And than modify your code

int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
      imageview.setBackgroundResource(mThumbIds[i]);}
Mitesh Jain
  • 673
  • 1
  • 7
  • 20
  • That will change the background on the same image multiple times. I don't think that's what is desired. – goto10 Oct 24 '11 at 06:17
  • He wants to change the background on a number of ImageViews depending on the value of currentPoi. If it's 3, then the first three (of 10) images should have their background changed. Your code had 5 background and, if currentPoi is 3, changes the background on one image 3 times. – goto10 Oct 24 '11 at 06:27
  • It would be like me asking you to paint the walls of a room red, green, blue, and orange and you ended up painting one wall red, then painting the same wall green, and then blue, and the orange. I'd end up with one orange wall instead of the four painted walls I asked for. – goto10 Oct 24 '11 at 06:29
  • @goto10, yes exactly..This is what my question is – tejas Oct 24 '11 at 06:33
  • My answer tells you how to do it. – goto10 Oct 24 '11 at 06:34
1

You could make an array of your ImageViews and then change them in your for loop.

ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);

and then change your for loop to:

for (i=1;i<=currentPoi;i++) {
   views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}

Arrays start at index 0, so make sure there's not an off-by-one error in here.

ImR
  • 787
  • 6
  • 8