Reputation: 10971
I'm trying to make a state list drawable to be used as a background for an image button.
and my state list selector is like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/play_button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/play_button_pressed" android:state_focused="true" />
<item android:drawable="@drawable/play_button_normal" />
</selector>
and the image button is like this:
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/play_button_bg"
/>
but the button appears as it's having the large image and the background, when clicking on it nothing happens. what can be wrong here?
Upvotes: 1
Views: 60
Reputation: 10971
Ok, I managed to resolve it using a state list selector with layer lists like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item>
<bitmap android:src="@drawable/play_button_pressed" android:gravity="center" />
</item>
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item>
<bitmap android:src="@drawable/play_button_pressed" android:gravity="center" />
</item>
</layer-list>
</item>
<item android:drawable="@drawable/play_button_normal" />
</selector>
The trick is in the bitmap's: android:gravity="center"
Upvotes: 0
Reputation: 39846
change it to:
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/play_button_bg"
android:background="@null"/>
backgrounds by default stretch to fit the view. So the two buttons are being changed, by the final looks is exactly the same.
Using as source and scaling them as "center without scaling", should show the two buttons changing (although the final effect might not be exact what u expect).
As a general rule state drawables gives less headache if all the drawables have the same size.
Upvotes: 1
Reputation: 3455
There is no issue with code. Since both images are identical, it looks like no change when we click on the image button. Change one image to some other png, say a pause button image & try. Also use ImageView in place of ImageButton.
Upvotes: 0