Ripple effect on Android Lollipop CardView Ripple effect on Android Lollipop CardView android android

Ripple effect on Android Lollipop CardView


You should add following to CardView:

android:foreground="?android:attr/selectableItemBackground"android:clickable="true"


Add these two line code into your xml view to give ripple effect on your cardView.

android:clickable="true"android:foreground="?android:attr/selectableItemBackground"


I managed to get the ripple effect on the cardview by :

<android.support.v7.widget.CardView     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:clickable="true"     android:foreground="@drawable/custom_bg"/>

and for the custom_bg that you can see in above code, you have to define a xml file for both lollipop(in drawable-v21 package) and pre-lollipop(in drawable package) devices.for custom_bg in drawable-v21 package the code is:

<ripple     xmlns:android="http://schemas.android.com/apk/res/android"    android:color="?android:attr/colorControlHighlight"><item    android:id="@android:id/mask"    android:drawable="@android:color/white"/></ripple>

for custom_bg in the drawable package, code is:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true">    <shape>        <solid android:color="@color/colorHighlight"></solid>    </shape></item><item>    <shape>        <solid android:color="@color/navigation_drawer_background"></solid>    </shape></item></selector>

so on pre-lollipop devices you will have a solid click effect and on lollipop devices you will have a ripple effect on the cardview.