Eric
Eric

Reputation: 488

Why my RelativeLayout doesn't use selector?

I have a RelativeLayout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/animated_bg">

and I've defined a selector

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_color">
<item>
    <selector>
        <item android:state_pressed="true"
            android:drawable="@color/grey_1" />

        <item android:state_focused="true"
            android:drawable="@color/grey_1" />

        <item android:drawable="@color/white" />
    </selector>
</item>

but when I press the layout it doesn't change color.

Anyone could notice why this is happening?

Update

Here is full xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/animated_bg">

    <RelativeLayout>

        <RelativeLayout>

            <ImageView />

            <LinearLayout>

                <TextView />

                <TextView />

            </LinearLayout>

        </RelativeLayout>

        <LinearLayout >

            <LinearLayout >

                <RelativeLayout >

                    <ImageView />

                    <TextView />

                    <TextView />

                    <TextView />

                </RelativeLayout>

                <RelativeLayout >

                    <ImageView />

                    <TextView />

                    <TextView />

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout >

                <RelativeLayout >

                    <TextView />

                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

Upvotes: 2

Views: 516

Answers (2)

h2nghia
h2nghia

Reputation: 452

I have tested your layout with its selector. It works well. However, I do not know if you have any other views/layouts inside your relative layout? If the RelativeLayout has child views. These views may consume your event press that will result the background does not change its color. In my testing, I do not have any child view inside my relative layout. Could you post your entire layout file?

UPDATE: If your child views do not need to handle the click event you can set android:clickable="false" so the click event will be passed to your parent relative layout. If it is not the case, then this link may help you.

Upvotes: 2

Rohit Sharma
Rohit Sharma

Reputation: 2007

In your xml selector color for android:state_pressed android:state_focused is same: @color/grey_1. Try to give different color for both.

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_color">
<item>
    <selector>
        <item android:state_pressed="true"
            android:drawable="@color/grey_1" />

        <item android:state_focused="true"
            android:drawable="@color/grey_1" />

        <item android:drawable="@color/white" />
    </selector>
</item>

Upvotes: 0

Related Questions