How to set state_selected in ripple drawable How to set state_selected in ripple drawable android android

How to set state_selected in ripple drawable


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>


To add to @Sohaib 's answer:

@Alanv is right that the OP didn't need a mask. But if one of your selector states is transparent and you need a mask it goes here:

<?xml version="1.0" encoding="utf-8"?><ripple    xmlns:android="http://schemas.android.com/apk/res/android"    android:color="@color/ripple_color">    <!-- mask here... -->    <item android:id="@android:id/mask">        <color android:color="@color/black"/> <!-- any color will do -->    </item>    <item>        <selector>            <!-- ... NOT here. -->            <item android:state_selected="true">                <color android:color="@color/blue"/>            </item>            <item android:state_activated="true">                <color android:color="@color/red"/>            </item>            <item>                <color android:color="@color/transparent"/>            </item>        </selector>    </item></ripple>

I initially had the mask inside my selector and :boom:


Combining the above answer with other answers from:

What should be the color of the Ripple, colorPrimary or colorAccent? (Material Design)

Gives a nice ripple effect which also works for when the item is in a selected state.

<ripple xmlns:android="http://schemas.android.com/apk/res/android"    android:color="?attr/colorControlHighlight"><!-- Ripple mask - applied to all selector states --><item android:id="@android:id/mask">    <color android:color="#42ffffff" /></item><!-- Selected state of item --><item>    <selector>        <item android:state_selected="true">            <color android:color="?attr/colorAccent" />        </item>    </selector></item></ripple>

This goes in your drawable-v21 folder, for other platforms you can just create a selector which uses the accent color:

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