Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Ripple Effect to RecyclerView item

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

    <android.support.v7.widget.RecyclerView     android:layout_width="match_parent"     android:layout_height="match_parent"     android:focusable="true"     android:clickable="true"     android:background="?android:selectableItemBackground"     android:id="@+id/recyclerView"     android:layout_below="@+id/tool_bar"/> 

This is the RecyclerView that I am trying to add the effect to:

enter image description here

like image 955
Georgi Koemdzhiev Avatar asked Jun 19 '15 06:06

Georgi Koemdzhiev


People also ask

How to set ripple effect on RecyclerView Android?

You should add the below attributes to your root element of the Recyclerview adapter view. The ? attr/selectableItemBackground informs to use the value of the attribute selectableItemBackground defined for the current theme”.

What is ripple effect in Android?

Overview. Ripple touch effect was introduced with material design in Android 5.0 (API level 21). Touch feedback in material design provides an instantaneous visual confirmation at the point of contact when users interact fwith UI elements.


1 Answers

I figured out. The only thing that I had to do is to add this attribute:

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

to the root element of the layout that my RecyclerView adapter inflates like that:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:paddingTop="8dp"     android:paddingBottom="8dp"     android:background="?android:attr/selectableItemBackground"     tools:background="@drawable/bg_gradient">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="17sp"         android:layout_marginLeft="15dp"         android:layout_marginStart="15dp"         android:id="@+id/shoppingListItem"         android:hint="@string/enter_item_hint"         android:layout_centerVertical="true"         android:layout_alignParentLeft="true"         android:layout_alignParentStart="true"/>      <CheckBox         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/shopping_list_item_checkbox_label"         android:id="@+id/shoppingListCheckBox"         android:layout_centerVertical="true"         android:layout_marginRight="15dp"         android:layout_alignParentRight="true"         android:layout_alignParentEnd="true"         android:checked="false"/>  </RelativeLayout> 

Result:

enter image description here

If you are still not able to see ripple effect, add these lines also to the root element of the layout.

android:clickable="true" android:focusable="true" 
like image 57
Georgi Koemdzhiev Avatar answered Oct 09 '22 04:10

Georgi Koemdzhiev