Issue
I'm trying to create a drawable that ripples when it's pressed, but when view.setSelected(true)
is called, it holds a solid color for a background.
The following file is placed in my drawable-v21 folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<color android:color="@color/green_primary" />
</item>
<item android:state_pressed="true">
<ripple android:color="@color/green_primary">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/green_selected" />
</shape>
</item>
</ripple>
</item>
</selector>
I've tried the above, and it holds a solid background when it's selected, but there isn't a ripple effect, rather just kind of a fade-in effect. If I just have the ripple with no selector, it ripples correctly when pressed, but obviously then I don't have a selected
state. How could I have both baked into a single background drawable?
Solution
What you need to do is:
1. Create a ripple_effect.xml file in drawable-v21
folder
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/green_primary">
<item android:drawable="@drawable/green_primary"/>
</ripple>
2. Create button_selector.xml file in drawable
folder to set the selected state color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/green_selected"/>
<item android:drawable="@drawable/ripple_effect"/>
</selector>
Answered By - iMDroid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.