14

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:selectableItemBackground" but it did not work.:

My Parent layout is like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dailyTaskList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:scrollbars="vertical" />
</LinearLayout>

and adapter template is shown in below

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:id="@+id/txtTitle"
                style="@style/kaho_panel_sub_heading_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txtDate"
                style="@style/kaho_content_small_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

Kindly give me solution

Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
Karthik Thunga
  • 1,093
  • 2
  • 12
  • 21

2 Answers2

46

Adding the android:background="?attr/selectableItemBackground" to the top most parent of your item layout should do the trick. However in some cases it still misses the animation, adding the android:clickable="true" does it.

<?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:background="?attr/selectableItemBackground"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/txtTitle"
            style="@style/kaho_panel_sub_heading_textview_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtDate"
            style="@style/kaho_content_small_textview_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>
Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42
3

In your recyclerView Item Parent, Add

android:background="?android:attr/selectableItemBackground"

Like below :

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:id="@+id/txtTitle"
                style="@style/kaho_panel_sub_heading_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txtDate"
                style="@style/kaho_content_small_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />


    </LinearLayout>

</LinearLayout>
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32