0

I am trying to pass an imagebutton as a reference through an indexed array.

I thought I could set the ID and then pass that ID as such:

MyButton=(ImageButton) findViewById(R.id.imageButton1);
MyButton.setId(0);

Then in my onClick I want to pass index "0":

MyButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {

    if (MyButton.isSelected()){
                Switch_Ctrl(0, OFF );
        }
});

index is passed to method:

boolean Switch_Ctrl(char button_num, byte state){

            button_num.setImageResource(R.drawable.switch_off);
            button_num.setSelected(false);
}

I get error can't resolve method setImageResource.

So I can't use the id "button_num". Not sure how I can reference the ImageButton?

  • `button_num` is a `char`. `char` has no such method as `setImageResource()`. I recommend some basic Java tutorials then come back and try again. If you want to reference a view by it's id number, then you have already done that! (`findViewById`) However, the correct solution is to make use of the instance of `View`, v, which is passed to the `onClickListener` which a reference to the button clicked. – Simon Nov 22 '13 at 15:58

1 Answers1

0

You can just store a reference to the button and change it onClick. Just make it an instance variable in your Activity or Fragment. You can declare a button seperately and then initialize it onCreate or onActivityCreated (once findViewById has become available). For example

private Button myButton;

@Override
protected void onCreate(Bundle savedInstanceState){
    myButton = (Button) findViewById(R.id.button);
    myButton.setOnClickListener(new Button.OnClickListener() {

         public void onClick(View v) {
             changeButtonBackground(v.getId());
         });

     }
 }

//if doing this for multiple butons, check which one
//was pressed and change the background
private void changeButtonBackground(int id){
    if(id == R.id.myButton){
        myButton.setBackground(R.drawable.whatever);
    }
}

Also - I wouldn't set the id of my button to anything else like 0. Your button already has an id, it's the one you defined in the XML as android:id="@+id/whateveritis". Unless you are creating view's programatically that do not exist in the XML (e.g. if you were adding TextView's to some parent layout on click of this button) you don't need to define a new id.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • I simplified my question, I have another method that I need to call Switch_Ctrl that is not an onCLick so I don't know the ImageButtonjust the ID....Regardless thanks for the help.. To get my imagebutton from id I just use "ImageButton button_id = (ImageButton) findViewById(button_num); – user1895526 Nov 22 '13 at 16:13
  • I'm confused - just call switch controll from the onClick. Is the issue that you don't know the id of the button so you don't know which button you need to change the background on? Or that you don't know which image to use for the background? – Rarw Nov 22 '13 at 16:24