6

How can I get the drawable of an Image Button to compare and do something if the drawable is A and something if is B?. Thank you so much.

    switch(getDrawableId(buttonRepeat)) {

        case R.drawable.a:
            mediaPlayer.setLooping(true);

             break;
        case R.drawable.b:
                mediaPlayer.setLooping(false);

             break;
        default:

        break;
          }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3714696
  • 91
  • 2
  • 8

1 Answers1

11

Use getDrawable() method in ImageButton and compare them using .getConstantState().equals()

Sample code:

ImageButton btn = (ImageButton) findViewById(R.id.myImageBtn);
Drawable drawable = btn.getDrawable();
if (drawable.getConstantState().equals(getResources().getDrawable(R.drawable.myDrawable).getConstantState())){
   //Do your work here
}

References:

http://developer.android.com/reference/android/widget/ImageButton.html

Comparing two drawables in android

Community
  • 1
  • 1
Carson Ip
  • 1,896
  • 17
  • 27