Muddassir Ahmed
Muddassir Ahmed

Reputation: 518

Android Recycler View Ripple Effect not working

I am using the following xml drawable as the background of my recyclerview list item.

touch_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"
        android:drawable="@color/green_text_500"/>
    <!-- Default, "just hangin' out" state. The base state also
         implements the ripple effect. -->
    <item android:drawable="@drawable/touch_selector_base" />
</selector>

touch_selector_base.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_grey">
    <item android:id="@android:id/mask" android:drawable="@color/light_grey" />
    <item android:drawable="@color/dark_grey"/>
</ripple>

In the list item I am using the touch_selector.xml as follows in my list_item_quote.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="86dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/touch_selector"
    >
    // layout
</LinearLayout>

SIMILARLY I have another xml drawable touch_selector_dark.xml and touch_selector_base_dark.xml

In my recycler-view-adapter I alternate between these two drawables based on the index. For even indexes I use the darker drawable and for the odd index I use the lighter background to produce an alternating effect. But the issue is that the ripple effect is not working.

Here are the colors:

light_grey = #5b5b5b

dark_grey = #212121

green_text_500 = #37863a

Upvotes: 1

Views: 2269

Answers (3)

ali zarei
ali zarei

Reputation: 1241

try this worked for me

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

Upvotes: 1

MrLeblond
MrLeblond

Reputation: 1035

I think your ripple file is not valid, you can check this:

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

You can use android:foreground="@drawable/touch_selector" to display the ripple effect and also use android:background="" to set any other drawable background.

Upvotes: 0

Vijay Chaudhary
Vijay Chaudhary

Reputation: 228

use this line in your recycleview list layout

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

Upvotes: 1

Related Questions