Skip to content Skip to sidebar Skip to footer

Imagebutton Selector Not Working

I am trying to set selector for ImageView, but it is not working My layout

Solution 1:

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.

Solution 2:

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).

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/btn_settings_inset_top_pressed"android:state_focused="true"android:state_pressed="true"/><itemandroid:drawable="@drawable/btn_settings_inset_top_pressed"android:state_focused="false"android:state_pressed="true"/><itemandroid:drawable="@drawable/btn_settings_inset_top"android:state_focused="true"android:state_pressed="false"/><itemandroid:drawable="@drawable/btn_settings_inset_top"android:state_focused="false"android:state_pressed="false"/></selector>

Solution 3:

I have solved it!

Inside my code i have

btnNoRecord.setOnTouchListener(newView.OnTouchListener() {
   @OverridepublicbooleanonTouch(View view, MotionEvent motionEvent)
   {
       ...
       returntrue;
   }
});

I change return true to return false and it is working!

Solution 4:

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

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"><shapeandroid:shape="rectangle"><solidandroid:color="@color/dark_orange_color"/><paddingandroid:bottom="5dp"android:left="5dp"android:right="5dp"android:top="5dp" /></shape></item><item><shapeandroid:shape="rectangle"><solidandroid:color="@color/orange_color"/><paddingandroid:bottom="5dp"android:left="5dp"android:right="5dp"android:top="5dp" /></shape></item></selector>

Solution 5:

In this case just swap attributes:

android:srcandroid:background

to

android:src="@drawable/selector"
android:background="@drawable/ic_action_record"

Hint. You can remove background by:

android:background="@null"

Post a Comment for "Imagebutton Selector Not Working"