Home » Android » android – Ripple effect on ListView item

android – Ripple effect on ListView item

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am new to Android development. I am trying to develop a SMS app for Android. I am successful with reading the inbox and contacts and display them in a ListView item. Now, what I want to achieve is to have a ripple effect on these ListView items every-time they are clicked or touched or selected. I used listSelector but seems it’s not working. If anybody could help me, below is my code:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/msglist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="true" 
        android:listSelector="#777777" />

How to&Answers:

You are going to inflate a layout in your Adapter for ListView, right? Then you can go to that layout.xml and set android:background="?android:selectableItemBackground" to the root element.

NOTICE: Ripple effect can only happen above API 21(Lollipop) by doing this. Under Lollipop, when you touch the item, a transparent blue layer will appear and overlay your source content, which is shown as selecting state.