11

I have a problem with finding buttons. I have an AlertDialog where I choose one of 5 options. When I choose an option I want change the color of the button I clicked. I declared buttons in xml file inside <RealativeLayout> but when I'm trying to find my button by id (id's are like "id1","id2"...)using the findViewById method, there is a mistake, which says that I can't use this method like I do:

AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Button btn_tmp;
        String theButtonId = "id";
        theButtonId = theButtonId+(String.valueOf(which));
        btn_tmp = (Button) findViewById(theButtonId);
    }
});

How can I fix that or maybe I should use other method?

EDIT:

I think I solved my problem. I used one of Button's method: getId() like this:

final int id = clickedButton.getId();
final ImageButton btn_tmp;
btn_tmp = (ImageButton)findViewById(id);
TheOddCoder
  • 161
  • 1
  • 12
Ziva
  • 3,181
  • 15
  • 48
  • 80

4 Answers4

4

Background: IDs are esentially variables

In the XML files we give IDs to the resources, and then the compilers use all these to generate the gen/R.java. So essentially, these IDs are int variables belonging in class R. An example of R.java:

// btw this file should never be edited!
public final class R {
  public static final class id {
    public static final int id1=0x7f0100aa;
    public static final int id2=0x7f0100ab;
  }
}

Just because a String exists that contains a valid name of a variable (R.id.id1), it can't magically access that variable. To do this, one can use reflection. However, in this case I believe it is an unnecessary complication, and will even be slower.

findViewById needs an ID (integer variable):

You cannot supply a String to this function. You should use an integer value, and specifically the ones that correspont to int variables in R.java. For example:

findViewById(R.id.id1) // for your 1st button

Solution:

You can dynamically select the integer value:

AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Button btn_tmp;
        int chosenID=-1;

        switch (which) {
        case 1:   chosenID=R.id.id1;
                  break;
        case 2:   chosenID=R.id.id2;
                  break;
        }
        btn_tmp = (Button) findViewById(chosenID);
    }
});

It is also suggested to use more explanatory IDs, like: buttonDoThis, buttonDoThat.

Community
  • 1
  • 1
Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • So is there any other method to find it? Because, if i have for example only a few buttons I use appropriate button using if(which==x) then... But if for example I have 1000 buttons it's really bad idea. So is there any other method? – Ziva Jan 11 '13 at 21:41
  • If you have 1000 buttons, first of all you have to bind them all, one by one! Binding process is achieved with the findViewById: you bind a variable with the actual button. Whats the point to have 1000 buttons, and bind(and actually use it later) only 1 button? **Can you explain us the problem with more detail?** – Paschalis Jan 11 '13 at 21:45
  • It was only an example, ofc I don't have 1000 buttons. I was only interesting about other solutions – Ziva Jan 11 '13 at 21:56
  • edited post, with an example of what you may be asking for! your can modify accepted answer there, and give it a try! – Paschalis Jan 11 '13 at 22:44
4
btn_tmp = (Button)findViewById(R.id.YourButtonId);

You are passing string instead of integer.

Ramesh Sangili
  • 1,633
  • 3
  • 17
  • 31
  • I tried this earlier, but it doesn't work. I think it doesn't work because in my code theButtonId id not a name which I used, but only a string which looks like the name of my button. – Ziva Jan 11 '13 at 21:36
  • go to your design layout and right click on the button and say Edit Id, there you can specify an id for the button and in java file, you can reference with the above code.. – Ramesh Sangili Jan 11 '13 at 22:06
1

If you are using your own layout.xml for the dialog, you need to inflate it first and give the view to your dialog builder.

LayoutInflater layoutInflater = (LayoutInflater) StartGameActivity.this.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

final RelativeLayout customDialogView= (RelativeLayout) layoutInflater.inflate(R.layout.custom_dialog_view, null);


AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
//give the custom view to your dialog builder.
builder.setView(customDialogView);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
    Button btn_tmp;

    switch(which){

    //....cases

    case 1:
         btn_tmp = (Button) customDialogView.findViewById(R.id.button1);
         //set the color of the button selected
         break;

    case 2:
         btn_tmp = (Button) customDialogView.findViewById(R.id.button2);
          //set the color of the button selected
         break;
    }

     //....cases
}
});
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
0

You are trying to use findViewById() with a String that holds the variable name of the ID. Java doesn't support this kind of dynamic variable names since variable names are only known at compile time, not run time. What are you trying to do? You will need to find another way to solve this problem. Please explain further and we can give suggestions.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268