TheDareDevil
TheDareDevil

Reputation: 129

How to enable highlight on listview items

I'm pretty much a noob in terms of Android development and I'm trying to highlight a listview item on touch like seen in most holo apps, but it just doesn't work for me.

My xml code is here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
android:orientation="vertical"
android:background="#ffdddddd"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">

<ListView android:id="@android:id/list" 
    android:scrollX="10.0dip" 
    android:background="#DDDDDD" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10.0dip"
    android:layout_marginTop="10.0dip" 
    android:layout_marginBottom="10.0dip"
    android:stackFromBottom="true"
    android:soundEffectsEnabled="true"
    android:listSelector="#0eBFE9"
 />

The problem is that the items in my listView aren't highlighted when selected. The only way to get it done is to use

android:drawSelectorOnTop = "true"

But then the text gets hidden while selecting. The app is for only v4.0+ if it helps.

Upvotes: 0

Views: 697

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 8939

For Highlighting an list item of ListView you need to use Selector concept.

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

For More details go through my Android Blog. and you will find out how to use selector on list item parent layout

http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

Thanks,

Upvotes: 1

Related Questions