I have an ImageButton and a LinearLayout wrapping that button, like this:
Set the selector to your LinearLayout
and add android:descendantFocusability="blocksDescendants"
to it.
In selector, replace all button backgrounds with layout backgrounds.
<LinearLayout
android:id="@+id/homeButtonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layout_bg_selector"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<ImageButton
android:id="@+id/homeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="@drawable/home_icon"
android:onClick="goBackToCameraScreen" />
</LinearLayout>
selector layout_bg_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#F0F0F0" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#00F000" /> <!-- focused -->
<item android:drawable="#0000F0" /> <!-- default -->
</selector>
I think the questioner is dead by now :XD
but this is the solution
add this two lines of code to your layout:
android:clickable = "true"
and
android:focusable = "true"
the main sample is
we have this constraint layout like :
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutSample"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="@drawable/layout_selector"
android:clickable="true"
android:focusable="true">
</androidx.constraintlayout.widget.ConstraintLayout>
and the ( layout_selector.xml ) like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_pressed" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="@drawable/bg_focused" android:state_focused="true" /> <!-- focused -->
<item android:drawable="@drawable/bg_default" /> <!-- default -->
</selector>
and for ( bg_pressed.xml ) and ( bg_focused.xml ) and ( bg_default.xml ) as we used in selector before we need to declare them in drawable folder like :
bg_pressed.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#ffffff" />
<stroke
android:width="2dp"
android:color="#00ff00" />
</shape>
bg_focused.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#ffffff" />
<stroke
android:width="2dp"
android:color="#ff0000" />
</shape>
bg_default.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#ffffff" />
</shape>
now put your image view or something else inside the layout and try some clicks :) (read the question!)
hope this will help you ...