Android background that pulsates when pressed, but solid color when selected?

I am trying to create a drawable that ripples when pressed, but when view.setSelected(true) is called, it contains a solid background color.

The following file is placed in the 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 tried above and it has a solid background when it is selected, but the ripple effect is not, but rather a kind of damping effect. If I only have ripple without a selector, it pulses correctly when pressed, but obviously then I have no selected state. How could I bake in the same background?

+5
source share
1 answer

What you need to do:

1. Create a ripple_effect.xml file in the drawable-v21

 <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/green_primary"> <item android:drawable="@drawable/green_primary"/> </ripple> 

2. Create a button_selector.xml file in the drawable folder to set the selected status 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> 
+10
source

All Articles