0

I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.

final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //toggle picture
        if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
            testButton.setBackgroundResource(R.drawable.alternatepicture);
        }   

        else {  
            testButton.setBackgroundResource(R.drawable.fakpicture);
        }
    }//end void onClick

});//end test button on click listener
Vladimir
  • 9,683
  • 6
  • 36
  • 57
user1011177
  • 1
  • 1
  • 2
  • You can see my solution here: [link][1] [1]: http://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state/14810912#14810912 – András Feb 11 '13 at 16:04

5 Answers5

1

try

testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));

However ToggleButton might suit your case better.

Vladimir
  • 9,683
  • 6
  • 36
  • 57
1

As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.

I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:

    final Drawable first = getResources().getDrawable(
            android.R.drawable.arrow_up_float);
    final Drawable second = getResources().getDrawable(
            android.R.drawable.arrow_down_float);

    final Button testButton = (Button) findViewById(R.id.toggleButton);
    testButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (testButton.getBackground().equals(first)) {
                testButton.setBackgroundDrawable(second);
            } else {
                testButton.setBackgroundDrawable(first);
            }
        }
    });
skynet
  • 9,898
  • 5
  • 43
  • 52
1

as the other friends answered , it is preferable to use the ToggleButton in Android ,

and in your case, if you want to keep your code , so your method should be like this :

final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 ) 
testButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //toggle picture
        if (status == 0) {
            testButton.setBackgroundResource(R.drawable.alternatepicture);
            status=1 ; // change the status to 1 so the at the second clic , the else will be executed
        }   

        else {  
            testButton.setBackgroundResource(R.drawable.fakpicture);
            status =0;//change the status to 0 so the at the second clic , the if will be executed
        }
    }//end void onClick

});//end test button on click listener
Houcine
  • 24,001
  • 13
  • 56
  • 83
0

You can use Buttons or Image buttons..

private ImageButton mod1,mod2;

mod1        = (ImageButton) findViewById(R.id.mod1);
mod2        = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);



public void onClick(View v) {

    mod1.getDrawable().clearColorFilter();
    mod2.getDrawable().clearColorFilter();

    switch (v.getId()) {
        case R.id.mod1:
            mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
            break;
        case R.id.mod2:
            mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
            break;
    }
}
András
  • 3,395
  • 1
  • 21
  • 27
0

You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.

Raz
  • 8,918
  • 3
  • 27
  • 40