Skip to content Skip to sidebar Skip to footer

How To Highlight Pressed Items In A Listview?

I am using this code below, but it doesn't work properly: If I select an item, the background is changed. But the background change also if I put only focus on the item without sel

Solution 1:

You can highlight/provide ripple effect to your list items using the following :

Create a selector item_ripple.xml in your drawable :

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"><shape><solidandroid:color="@color/activated_color"></solid></shape></item><item ><shape><solidandroid:color="@android:color/transparent"></solid></shape></item></selector>

Create a selector item_ripple.xml in your drawable-v21

<?xml version="1.0" encoding="utf-8"?><ripplexmlns:android="http://schemas.android.com/apk/res/android"android:color="@color/activated_color"><itemandroid:id="@android:id/mask"android:drawable="@android:color/white" /></ripple>

You need to add these selector as background of your item layout :

android:background="@drawable/item_ripple"

Solution 2:

You can use pressed state in your selector file

/drawable/list_selector.xml

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@android:color/holo_red_light"android:state_pressed="true"/></selector>

then set following attribute in your listView

android:listSelector="@drawable/list_selector"

Post a Comment for "How To Highlight Pressed Items In A Listview?"