I am trying to set selector for ImageView, but it is not working
My layout
<LinearLayout
android:id="@+id/actionLayout"
android:orientation="horizontal"
android:background="@color/orange_color"
android:gravity="center_vertical"
android:paddingTop="@dimen/actions_top_bottom"
android:paddingBottom="@dimen/actions_top_bottom"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="@dimen/actions_height"
android:id="@+id/btnNoRecord"
android:scaleType="centerInside"
android:src="@drawable/ic_action_record"
android:onClick="onRecordSwitcherClick"
android:layout_weight="1"
android:background="@drawable/selector"/>
...
</LinearLayout>
My selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/dark_orange_color"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/orange_color"/>
</shape>
</item>
</selector>
Where is bug? Maybe there is something bug inside LinearLayout properties?
EDIT I just found ImageButton outside LinearLayout working fine. But i'm really needed layout
Answer: You can not display text with imageButton . Method that tell in Accepted answer also not work. If you use android:drawableLeft="@drawable/buttonok" then you can not set drawable in center of button . If you use android:background="@drawable/button_bg" then color of your drawable will be changed.
Set clickable
attribute in your ImageButton
as shown below:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="@dimen/actions_height"
android:id="@+id/btnNoRecord"
android:src="@drawable/ic_action_record"
android:background="@drawable/selector"
android:clickable="true"/>
ImageButton
is extended from ImageView
. The clickable
attribute is false by default so you have to manually set it.
Your selector doesn't look correct.
I think you might be missing the states (ie. state_focused="true", etc). It is important to remember that the selector is analyzed in order from top to bottom (so if a state is encountered first, the other items in the selector will be ignored - ie. order matters).
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/btn_settings_inset_top_pressed"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@drawable/btn_settings_inset_top_pressed"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@drawable/btn_settings_inset_top"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@drawable/btn_settings_inset_top"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
put padding in your imageview to see the background selector
<ImageButton
android:layout_width="wrap_content"
android:layout_height="@dimen/actions_height"
android:padding="5dp"
android:id="@+id/btnNoRecord"
android:scaleType="centerInside"
android:src="@drawable/ic_action_record"
android:onClick="onRecordSwitcherClick"
android:layout_weight="1"
android:background="@drawable/selector"/>
OR
Try it like this , this will work you have to give padding so that background selector is visible
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/dark_orange_color"/>
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/orange_color"/>
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
</selector>
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