72

I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).

My question is: How can I use findViewById without statically having to type in each button id? Here is what I would like to do:

    for(int i=0; i<some_value; i++) {
       for(int j=0; j<some_other_value; j++) {
        String buttonID = "btn" + i + "-" + j;
        buttons[i][j] = ((Button) findViewById(R.id.buttonID));
        buttons[i][j].setOnClickListener(this);
       }
    }

Thanks in advance!

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
user573536
  • 2,335
  • 3
  • 17
  • 8

8 Answers8

127

You should use getIdentifier()

for(int i=0; i<some_value; i++) {
   for(int j=0; j<some_other_value; j++) {
    String buttonID = "btn" + i + "-" + j;
    int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    buttons[i][j] = ((Button) findViewById(resID));
    buttons[i][j].setOnClickListener(this);
   }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
6

You can try making an int[] that holds all of your button IDs, and then iterate over that:

int[] buttonIDs = new int[] {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

for(int i=0; i<buttonIDs.length; i++) {
    Button b = (Button) findViewById(buttonIDs[i]);
    b.setOnClickListener(this);
}
Rick Barkhouse
  • 1,186
  • 2
  • 10
  • 15
3

Take a look at these answers:

Community
  • 1
  • 1
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 1
    @user573536 be aware that using `getResources().getIdentifier()` in a loop can cause performance to degrade when you have a large number of lookups. – dave.c Feb 01 '11 at 16:47
  • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells). – Cristian Feb 01 '11 at 17:03
1

you can Use tag if you want to access.

in onClick

int i=Integer.parseInt(v.getTag);

But you cant access that button like this.

simply create button programatically

by Button b=new Button(this);

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
0

create Custom Button in java code rather in Xml as i shown below

Button bs_text[]= new Button[some_value];

    for(int z=0;z<some_value;z++)
        {
            try
            {

            bs_text[z]   =  (Button) new Button(this);

            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                Log.d("ArrayIndexOutOfBoundsException",e.toString());
            }
        }
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
0

If your top level view only has those button views as children, you could do

for (int i = 0 ; i < yourView.getChildCount(); i++) {
    Button b = (Button) yourView.getChildAt(i);
    b.setOnClickListener(xxxx);
}

If there are more views present you'd need to check if the selected one is one of your buttons.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with `getResources().getIdentifier()`. – dave.c Feb 01 '11 at 16:58
  • No sorry no idea. A view knows his children, so I don't think it is too bad. – Heiko Rupp Feb 01 '11 at 21:26
0

If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.

int id = 0;

switch(name) {
    case "x":
        id = R.id.x;
        break;
    etc.etc.
}

String value = findViewById(id);
B.Cakir
  • 567
  • 1
  • 5
  • 19
0

To put it simply, here's a function for it

public View findViewByArrayName (String name, int i) {
        buttonID = name + Integer.toString(i);
        resID = getResources().getIdentifier(buttonID, "id", getPackageName());
        return findViewById(resID);
    }

Also unlike Python, Java is a compiled language, so it probably makes sense that there aren't any chances for dynamic variable names. Unless achieved through a certain approach like this one.

Mihkuno
  • 99
  • 2
  • 9