4

I have sixteen image buttons, so I create an array of Image Buttons. I would like to initialize them with a for loop, I would not like to do that one by one, though I do not know if it is possible. For one imagebutton, it would be something, like this:

imgbtn[0] = (ImageButton)findViewById(R.id.imgButton1);

I see that the R.id.smth part is an integer. Does it say somewhere, where does that integer value start for imgButtons? So that I could so something like this:

int value = R.id.imgButton1;
for(int i = 0; i < imgbtn.length; i++)
{
     imgbtn[i] = (ImageButton)findViewById(value);
     value++;   //or something along these lines
}
masm64
  • 1,222
  • 3
  • 14
  • 31

2 Answers2

7

ID's aren't assigned incrementally, and are in fact quite random, so it is impossible to guess at what the resource id will be. What you need is to actually get the id dynamically via the getIdentifier() method

int value = 1;
for(int i = 0; i < imgbtn.length; i++){
    int resId = getResources().getIdentifier("imgButton" + value, "id", this.getPackageName());
    imgbtn[0] = (ImageButton) findViewById(resId);
    value++;
}

The getResources().getIdentifier(tag, type, package) will return the actual int value stored in your resources (R) for your images. You can use that to find your views dynamically.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
3

No the above code won't work because the integer assigned isn't necessarily incremented by one. Also, the integers are randomly generated when the program is built.

If you look at your gen/R.id file you'll see something like this:

public static final class id {
    public static final int action_settings=0x7f080001;
    public static final int dimensionPlusView=0x7f080000;
}
public static final class layout {
    public static final int activity_main=0x7f030000;
}
public static final class menu {
    public static final int main=0x7f070000;
}
public static final class string {
    public static final int action_settings=0x7f050001;
    public static final int app_name=0x7f050000;
    public static final int photo_credit=0x7f050002;
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • 3
    Beat me to the punch. +1. Building on what Blaine said, the integers are randomly assigned. They will also keep changing if you add more widgets / resources. These are automatically created in the `R.java` file that is created with the Android project. – rayryeng May 02 '14 at 19:03
  • 1
    Don't mess with ids directly - use the `getResources().getIdentifier()` method instead – Phantômaxx May 02 '14 at 19:05
  • 1
    I'm not suggesting that you mess with these, but rather showing that they are not "integers" (1,2,3,4, etc). – BlackHatSamurai May 02 '14 at 19:06
  • I know, Blaine, I just added my personal advice as a completion. ;). By the way, they **are** integers (long integers), in the form of hexadecimal numbers. – Phantômaxx May 02 '14 at 19:08
  • 1
    No worry - you have my penultimate +1 of the day. ;) – Phantômaxx May 02 '14 at 19:10
  • 1
    @BobMalooga if I could give you a +1 for the use of `penultimate` I would... Lol... – BlackHatSamurai May 02 '14 at 19:13
  • As you did. Thank you, dear! Sorry, but I also finished my votes on comments... :) It's the end of the day, here. – Phantômaxx May 02 '14 at 19:13