Highlight ListView selected row

user611089 picture user611089 · Feb 20, 2011 · Viewed 87.8k times · Source

I have a list of albums (several hundred). When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No problem. However, after touching the ablum in albumListView I want the row to remain highlighted so the user knows which item they have clicked on, and can then move to OK or PLAY. How do I highlight a row in ListView?

Answer

Hiep picture Hiep · Oct 31, 2012

Other solution (mostly in XML)

1) set the choiceMode of the ListView to singleChoice

<ListView
    android:choiceMode="singleChoice"
.../>

2) Items of the list must be a Checkable View and use a Color State List as Background

eg: album_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@drawable/list_selector"
.../>

3) the background Color State (list_selector.xml) defines the highlight color

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