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;
}
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.
android:src This sets a drawable as the content of this ImageView.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With