1

Possible Duplicate:
Android findViewbyId with a variant string

I have this type of code within an Activity:

ImageButton button1 = (ImageButton)findViewById(R.id.butid1);
ImageButton button2 = (ImageButton)findViewById(R.id.butid2);
ImageButton button3 = (ImageButton)findViewById(R.id.butid3);

Now I want to declare these variables within a for-loop. Also, actions will be taken with these N buttons. For example:

for (int NUMBER = 1; NUMBER <= N; NUMBER++) {
    ImageButton button"NUMBER" = (ImageButton)findViewById(R.id.butid"NUMBER");
    button"NUMBER".setEnabled(false);
}

N will change depending on various situations. What can I do best to solve this? (P.S. Couldn't find the result in the "Questions with similar titles")

Community
  • 1
  • 1
schijndelvanjos
  • 79
  • 3
  • 10
  • Why don't you use an array of buttons? Or arraylist If N is variable – prongs Dec 31 '11 at 11:19
  • I had done a small starting program for Android, hadn't done much on it. But cann't you use Arrays like ImageButton[] button = new ImageButton[3]; and then use button[i] = (ImageButton)findViewById(R.id.butid + Integer.toString(i)); Regards – nIcE cOw Dec 31 '11 at 11:21
  • Same question here: http://stackoverflow.com/questions/6679434/android-findviewbyid-with-a-variant-string – molnarm Dec 31 '11 at 11:25
  • 2
    There's no need to down vote the question as the user is obviously new to stackoverflow and Android development. – Flo Dec 31 '11 at 11:32
  • @MártonMolnár Well I can't find it if the title is not equal to mine. – schijndelvanjos Dec 31 '11 at 11:35
  • @schijndelvanjos Anyway, now you have the answer for your question ;-) I googled for "findviewbyid string". – molnarm Dec 31 '11 at 11:39
  • you can not write for loop for (ImageButton button2 = (ImageButton)findViewById(R.id.*); ). As R.id.build(*) is build by compiler and it takes the reference from the property which u have defined for controls. Better resolution is just declare the ARRAY of BUTTONS outside for loop. Assign value to each array element. Once u will have all the data outside. Try to make for loop for array and assign value of (setEnabled) to iterate over all elements. Let me know whether you are getting my point or not... – Developer Dec 31 '11 at 14:40
  • @Mishal At this moment, I used the solution proposed by Flo shown below. The problem is, that I have to load my XML file before I can use the FindViewById function. This means that every time I change the layout XML, I have to recreate the array of buttons locally. I cannot declare the buttons as public static final variables at the start, because Android cannot find them. Also in the onCreate function I have a problem, since the buttons are not in the layout I want to start with. – schijndelvanjos Dec 31 '11 at 15:44

2 Answers2

2

Before the loop, put all you Buttons into a Button array. So you could simply iterate over the array as you like.

ImageButton[] buttons = new ImageButton[<put here the total number of Buttons>];

buttons[0] = (ImageButton)findViewById(R.id.butid1);
buttons[1] = (ImageButton)findViewById(R.id.butid2);
...
buttons[N-1] = (ImageButton)findViewById(R.id.butidN);

for (int i = 0; i < N; i++) {
    buttons[i].setEnabled(false);
}
Flo
  • 27,355
  • 15
  • 87
  • 125
0

Use ImageButton[] button = new ImageButton[NUMBER] and then your for loop should be:

for (int i = 0; i < NUMBER; i++) {
    ImageButton button[i] = (ImageButton)findViewById(R.id [***]);
    button[i].setEnabled(false);
}

[*] I don't know Android, but I imagine there is a way to get all of the buttons as some kind of list or array so that you don't need to access them each by Id.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57