9

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" />

Jared Rummler
  • 35,743
  • 18
  • 127
  • 142
Clyde Winux
  • 265
  • 2
  • 11
  • try search RippleDrawable which available since api level 21, and try to make it available on your desire platform. – VinceStyling Aug 26 '15 at 02:43
  • @VinceStyling have tried RippleDrawable but still it doesn't work. Take note that I am using a custom theme for my activity. If I use AppThemes ripple effect works, but when I apply a custom theme there's no ripple effect anymore. – Clyde Winux Aug 26 '15 at 03:57
  • Please check http://stackoverflow.com/a/26604471/2414129 – ColinWa Dec 07 '16 at 11:52

1 Answers1

18

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.

Defuera
  • 3,903
  • 2
  • 25
  • 36
  • 5
    If your root element is `FrameLayout` or subclass of it such as `CardView`, you can set `android:foreground="?android:attr/selectableItemBackground"` instead. As a result, you can set color of it easily by setting background. – ywwynm Aug 26 '15 at 04:57