Karthik Thunga
Karthik Thunga

Reputation: 1113

Adding Ripple effect to Recycler list view for both lollipop and pre lollipop

I am trying to add Ripple Effect to RecyclerView's item. I had a look online, but could not find what I need. I have tried android:background attribute to the RecyclerView itself and set it to "?android:attr/selectableItemBackground" but it did not work.

My adapter template is like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    xmlns:custom="http://schemas.android.com/apk/res-auto">


    <LinearLayout
        android:id="@+id/anLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:weightSum="4">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="3">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                style="@style/kaho_listview_label_heading"
                android:id="@+id/txtTitle"
                custom:CustomTextViewFont="@string/kaho_segoeui_semi_light_font"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:maxLines="2"
                android:id="@+id/txtDetail"
                style="@style/kaho_content_small_textview_style"
                custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"/>

             </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="7dp"
            android:layout_gravity="center"
            android:gravity="right">

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:id="@+id/txtDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/kaho_content_small_textview_style"
                custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
                />

        </LinearLayout>
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:indeterminate="true"
            android:visibility="gone"
            style="@android:style/Widget.Holo.ProgressBar"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>

</LinearLayout>

Please help me

Upvotes: 0

Views: 751

Answers (2)

Dipali Shah
Dipali Shah

Reputation: 3798

Use this library it provide support for both lollipop and pre lollipop versions.And for better understanding about implementation is shown in below link.

https://github.com/traex/RippleEffect

compile line for this library

dependencies {
compile 'com.github.traex.rippleeffect:library:1.3'
}

Upvotes: 1

Genehme
Genehme

Reputation: 69

In drawable folder add new xml file like named "ripple_effect" with:

 <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/ripple_color">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="@color/button_background_color" />
        </shape>
    </item>
</ripple>

u can implement this in xml like background for items or in java like setResourceBackground also u can use standard android implementation:

android:background="?attr/selectableItemBackground"

Upvotes: 0

Related Questions