ImageButton selector not working

I am trying to set a selector for ImageView, but it does not work

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 the mistake? Maybe there is something in the properties of LinearLayout?

EDIT I just found that ImageButton outside LinearLayout is working fine. But I really need a layout.

+2
source share
4 answers

I solved it!

Inside my code, I have

 btnNoRecord.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { ... return true; } }); 

I change returns true to returns false , and it works!

0
source

Set the clickable attribute in 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 expanding from ImageView . The clickable attribute is false by default, so you must manually set it.

+2
source

Your selector does not look right.

I think you might be missing states (for example, state_focused = "true", etc.). It is important to remember that the selector is analyzed in order from top to bottom (therefore, if a condition is first encountered, other elements in the selector will be ignored, i.e. Order issues).

 <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> 
+1
source

add a space to your image to see the background selector

  <ImageButton android:layout_width="wrap_content" android:layout_height="@dimen/actions_height" android:paddingn="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 this, it will work, you must indent so that the background selector is displayed

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

0
source

Source: https://habr.com/ru/post/1333359/


All Articles