How to set state_selected in ripple drawable

Sohaib picture Sohaib · Dec 16, 2014 · Viewed 24.6k times · Source

How to specify android:state_selected inside a RippleDrawable

I have following xml for ripple drawable but background color doesn't show up when I set myView.setSelected(true);

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#DDDDDD"
    >

    <item android:id="@android:id/mask">
        <shape>
            <solid
                android:color="@color/black" />
        </shape>
    </item>


    <item android:state_selected="true">
        <shape>
            <solid
                android:color="#EEEEEE" />
        </shape>
    </item>


    <item>
        <color android:color="#FFFFFF" />
    </item>

</ripple>

Answer

Sohaib picture Sohaib · Dec 16, 2014

Found the answer, just in case someone else having the same problem

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#DDDDDD"
    >


    <item>
        <selector>
            <item android:state_selected="true">
                <color android:color="#EEEEEE" />
            </item>

            <item android:state_activated="true">
                <color android:color="#EEEEEE" />
            </item>

            <item>
                <color android:color="#FFFFFF" />
            </item>
        </selector>
    </item>


</ripple>