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

12,984

Solution 1

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>

Solution 2

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

Share:
12,984
Bitcoin Cash - ADA enthusiast
Author by

Bitcoin Cash - ADA enthusiast

Bitcoin Cash donations: bitcoincash:qpzkuqj5j0f4f6h5q7j2qnzuwlj0prat8c8xek3ndn ADA donations: addr1q94a6fs54ktss7k8yezsd9y3w6f7jlz2wa9zjxe7zlh869636m56ftcgfwp5ernjfu422dh3869hhnksyvn9dsla4gjqzl92xe

Updated on June 04, 2022

Comments

  • Bitcoin Cash - ADA enthusiast
    Bitcoin Cash - ADA enthusiast almost 2 years

    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!! =)