Reputation: 295
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" />
Upvotes: 12
Views: 11787
Reputation:
You are going to inflate a layout in your Adapter
for ListView
, right? Then you can go to that layout.xml and set the background of the root element to:
android:background="?android:attr/selectableItemBackground"
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.
Upvotes: 21
Reputation: 514
For enabling the ripple effect for list items via the XML layout, the following attributes need to be set in the root layout:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
With only the android:backgroudn
attribute the ripple effect might not show up on click.
More information can be found in Customize Touch Feedback (page contains deprecation warning for the other options).
Upvotes: 2