2

I'm developing a material design interface. I have cards that I want to have the ripple effect when clicked. I've done what google says and nothing happens. Here's my code:

<android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:clickable="true"
            android:layout_width="fill_parent"
            android:layout_height="250dp"
            android:elevation="5dp"
            android:background="?android:selectableItemBackground">
            >

            <ImageView 
                android:id="@+id/blue_image"
                android:src="@drawable/blue"
                android:layout_alignParentTop="true"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:elevation="8dp"
            />

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"    
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:text="And this is a blue card!" 
                android:elevation="10dp"
                />
Cnorwood7641
  • 336
  • 1
  • 5
  • 17

2 Answers2

3

Take a look at my example (API<21). It's similiar to Lollipop ripple effect.

https://www.youtube.com/watch?v=liure-_hlX0&feature=youtu.be

For this demonstrantion i have used this libary: https://github.com/balysv/material-ripple

just add to build.gradle:

 compile 'com.balysv:material-ripple:1.0.1'

Selector for Card View (wrapped RelativeLayout): list_item_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:drawable="@color/ripplecolorbackground" android:state_pressed="true"/>
<!-- selected -->
<item android:drawable="@color/ripplecolorbackground" android:state_selected="true"/>
<!-- focused -->
<item android:drawable="@color/ripplecolorbackground" android:state_focused="true"/>
<!-- default -->
<item android:drawable="@color/transparent"/>

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3dp"
    card_view:cardPreventCornerOverlap="true">

    <com.balysv.materialripple.MaterialRippleLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mrl_rippleColor="@color/rippplecolorlist"
        app:mrl_rippleAlpha="0.10"
        app:mrl_rippleOverlay="false"
        app:mrl_rippleHover="true"
        app:mrl_rippleDimension="1dp"
        app:mrl_rippleDuration="350">

    <RelativeLayout
        android:background="@drawable/list_item_selector"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

            </com.balysv.materialripple.MaterialRippleLayout>
    </android.support.v7.widget.CardView>

Colors:

<color name="ripplecolorbackground">#1A000000</color>
<color name="rippplecolorlist">#26000000</color>
Djordje Tankosic
  • 1,975
  • 2
  • 20
  • 21
0

I created a library that allows to customize every view without affecting the layout to create the ripple effect

I hope it helps you https://github.com/xgc1986/RippleViews/blob/master/docs/RippleDrawableHelper.md

Also the sample is using cardViews with this effect ;)

xgc1986
  • 868
  • 6
  • 8