Android - How can I create a selector for Layouts (like an ImageButton selector)

I have an ImageButton and a LinearLayout wrapping that button, like this:

<LinearLayout
        android:id="@+id/homeButtonLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/p_buttons_background"
        android:orientation="vertical" >

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

I put a background around the ImageButton in the LinearLayout, as you can see above.

I want to be able to change that background (that is, the background of the LinearLayout, not the background of the ImageButton) when the user clicks on the ImageButton. I could do that programmatically, but I want to do that via XML. I could easily change the background of the ImageButton by writing a selector, like this:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
       android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
       android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

That's EXACTLY what I want, except that I want to change the LinearLayout background, and not the ImageButton background. How can I create a XML selector just like the one above, but that actually changes the background of the LinearLayout instead of the ImageButton background??

Thank you in advance!! =)

Upvotes: 2

Views: 15007

Answers (2)

A S A D I
A S A D I

Reputation: 159

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

Upvotes: 1

Ron
Ron

Reputation: 24235

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>

Upvotes: 4

Related Questions