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>
Found the answer, just in case someone else having the same problem
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:
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.
This goes in your drawable-v21 folder, for other platforms you can just create a selector which uses the accent color:
I wanted to mimic the behavior of the Material Design checkboxes but I couldn't get them right until I used a ColorStateList like this:
In drawable-v21/bg_checkbox_ripple.xml
In color/checked_accent_statelist.xml
The attribute "?android:textColorHighlight" is your accent color but with the right transparency for using it in ripples (I think it is 26%).
Also, you should provide a fallback for pre API 21 devices in drawable/bg_checkbox_ripple.xml