Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get drawable of image button

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;
          }
like image 533
user3714696 Avatar asked Jun 08 '14 14:06

user3714696


People also ask

How to put image in Drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.

Which tag helps you draw image button on activity?

android:src This sets a drawable as the content of this ImageView.

How to add image Drawable android?

Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.

How will you add graphics to button?

Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file.


1 Answers

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

like image 121
Carson Ip Avatar answered Sep 28 '22 09:09

Carson Ip