I have a question that is driving me crazy.
I have a large number of buttons (10, more or less) on my screen, inside a TableRow.
I need to access them, and I had planned to perform through a loop.
Access to one, is very easy, adding this:
boton7 = (Button) findViewById (R.id.Btn7)
My question is, if you can dynamically set the id string (R.id.Btn7) to put in a can get the buttons for, and for example, change the color …. something like this:
for (int i = 0; i <10; i + +) {
Button eachBoton= (Button) findViewById (R.id.Btn + i);
eachBoton. setBackgroundColor (Color.Red);
}
That, of course, does not work …. my question is if anyone knows how exactly the chain can be mounted
R.id.Btn + i
to work.
Thanks a lot.
You can use
Resources#getIdentifier()to get a resource identifier for the given resource name:Alternately you can prepare an array with all the ids you need and access elements of that array. This is more efficient: