I couldn’t find a solution to this yet.
I have a mute/unmute button. I set it up as an ImageButton, with both src and background being selector drawables.
I am displaying
– the “muted” icon when the button is selected
– the “mute” icon when the button is not selected
When the button is clicked I would like to display a yellow background for a fraction of a second
but what I experience is that:
the yellow background appears (by clicking) only when the button is not selected (isSelected=false), and it doesn’t appear when the button is selected (isSelected=false)
I don’t understand why, but that’s what happens!
The yellow background does appear when the button is kept pressed (no matter whether is selected or not), but what I am really interested in is to make it appear when you just click on it without the need of keeping it pressed
any solution?
view.java
volumemuteImageButton = (ImageButton) findViewById(R.id.volume_mute);
volumemuteImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
volumemuteImageButton.setSelected(!volumemuteImageButton.isSelected());
}
});
layout.xml
<ImageButton
android:id="@+id/volume_mute"
android:layout_width="58dp"
android:layout_height="fill_parent"
android:src="@drawable/img_selector"
android:background="@drawable/bg_selector" />
img_selector.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/sdmp_ic_muted_pressed"
android:state_selected="true" android:state_pressed="true" />
<item android:drawable="@drawable/sdmp_ic_muted_pressed"
android:state_selected="true" android:state_focused="true" />
<item android:drawable="@drawable/sdmp_ic_muted_default"
android:state_selected="true" />
<item android:drawable="@drawable/sdmp_ic_mute_pressed"
android:state_selected="false" android:state_pressed="true" />
<item android:drawable="@drawable/sdmp_ic_mute_pressed"
android:state_selected="false" android:state_focused="true" />
<item android:drawable="@drawable/sdmp_ic_mute_default"
android:state_selected="false" />
</selector>
bg_selector.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/sdmp_color_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/sdmp_color_yellow"
android:state_focused="true" />
</selector>
I SOLVED IT!!!!!
I don’t know why, but using the
onTouchACTION_DOWNwould do it, while using theonClickthestate_pressedgets fired only when passing from theselected=falseto theselected=truehow it was before:
how it is now: