43

I have a list view contactslist. I wrote the code for highlighting the selected item in the ListView. It is working. When I click on one item it is highlighting that item, but the problem is if I click on another item it is highlighting that one too. I want to highlight the selected item only. The previous selection should go away when I click on another item.

arg1.setBackgroundResource(R.drawable.highlighter);

This is the code in the click listener using to highlight the selected item. How can I fix this?

I'm setting the background of the rows in the adapter:

public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF}; 
public int colorPos; 

[...]
colorPos = position % colors.length; 
row.setBackgroundColor(colors[colorPos]);
Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
andro-girl
  • 7,989
  • 22
  • 71
  • 94
  • if the answer is acceptable i can accept dat.but if the answer doesnt help me den how can i accept that? – andro-girl May 02 '11 at 06:38
  • If you're not satisfied with the answers, just forget the boost part of my previous comment. But some more information would really help us to figure out where your problem roots, and how to help you. thank you! – rekaszeru May 02 '11 at 06:49
  • i am using relative layout.i am setting the background for the list view using adapter,my logic is if the rawnum is an odd one give background color and if it s even number give another background color.below is the logic implementation.public int[] colors = new int[]{ 0xFFedf5ff, 0xFFFFFFFF}; public int colorPos; colorPos = position % colors.length; row.setBackgroundColor(colors[colorPos]); – andro-girl May 02 '11 at 07:36
  • please from now on rather edit your question to share code snippets, than paste them in comments, since here they are not quite readable. It would also be helpful, if you'd provide the code in which you handle the selected background. thanks! – rekaszeru May 02 '11 at 07:45
  • thanks....but not yet got d solution.i m stuck on this.plz help me – andro-girl May 02 '11 at 07:58
  • I've posted an answer based on what i understood from your question, I hope I got it right, and can help solve your problem. – rekaszeru May 02 '11 at 08:01
  • For future references, this works: http://www.michenux.net/android-listview-highlight-selected-item-387.html – RedGlyph Aug 31 '13 at 14:40

4 Answers4

124

ListViews by default don't have a choiceMode set (it's set to none), so the current selection is not indicated visually.

To change this, you just need to set the choiceMode attribute of your ListView to singleChoice.
If you'd like custom background for the selected items in your list, you should also set the listSelector attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).

<ListView android:id="@+id/my_list"
        android:choiceMode="singleChoice" 
        android:listSelector="@android:color/darker_gray" />

If you don't use a ListView directly, but a ListActivity, then these attributes need to be set from code, so you should extend your activity's onCreate method with these lines:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.darker_gray);

So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.

Reply to the update

If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • i tried this code,but if i make the listview choice_mode_single den the first selected item is highlighting,but if i selct another item it s not highlighting. – andro-girl May 02 '11 at 08:37
  • 1
    is there any method or proprty to get previous selected item in the listview? – andro-girl May 02 '11 at 08:40
  • did you remove the code where you set the background using an onitemclick listener? – rekaszeru May 02 '11 at 10:52
  • I'm using `android.support.v4.app.ListFragment`. adding the line `getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);` in onCreate() gives me Exception: `java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.view.InflateException: Binary XML file line #90: Error inflating class fragment` – PC. May 08 '12 at 06:36
  • I've tried many combinations in the past but encountered the issue again recently. I wonder if it's device specific, my 2.3 device had the same issue as Youngjae was having. – Philio May 14 '14 at 22:37
  • It works great, but getSelectedItem returns null, and if dig deeper, then getSelectedItemPosition returns -1. How to make these methods return a correct value? – user2305886 Aug 22 '20 at 02:22
29

In the listview xml add the "singleChoice" mode

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    (...) >
</ListView>

In the list item layout add

android:background="?android:attr/activatedBackgroundIndicator

example

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

     <!-- your item content-->

</LinearLayout> 
  • 1
    Why is this not the first thing that comes up on Google!? I've been trying for hours to get this to work. I tried all sorts of states and combinations of states (selected, activated, focused, etc..) in a selector drawable with `android:listSelector="@drawable/theSelector"` set but when it worked it was buggy (the selection indicator would sort of stay stuck while i scrolled the list, even staying in between two items sometimes). This finally made it work properly. THANK YOU. – Danilo Carvalho Feb 09 '16 at 04:01
  • I love one-liners... Thank you very much! – Smitty-Werben-Jager-Manjenson Feb 13 '19 at 20:38
8

A better way is in your theme, @drawable/list_selector

list_selector.xml :

<!-- <item android:drawable="@color/android:transparent"  android:state_selected="true" /> -->
<item android:drawable="@color/list_bg" android:state_selected="true"/>
<item android:drawable="@color/list_bg" android:state_activated="true"/>
<item android:drawable="@color/transparent"/>

then set background for root of your list_row.xml android:background="?android:attr/activatedBackgroundIndicator"

subair_a
  • 1,028
  • 2
  • 14
  • 19
0

Try this at onListItemClick

view.getFocusables(POSITION);
view.setSelected(true);

It highlights the selection.

njzk2
  • 38,969
  • 7
  • 69
  • 107
kanna
  • 27
  • 3