How to highlight selected item in ListView?
Asked Answered
M

6

11

I know that android doesn't highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show details on the right side of the activity(wonder how Google did that).

So the story is I have to highlight what's been selected on the left side ListView. I've found some similar questions and the solutions are basically:

1.override the adapter's getView method and setBackground for selected position

2.setBackground of the view onItemClick and clear it for anther selection

But none of them worked for me due to a weird behaviour: As I click on one item and highlight it, the fifth item after it is highlighted as well, and so on so forth as I scroll down the list.

Any suggestions? THX!

Microlith answered 19/12, 2011 at 18:54 Comment(0)
Z
23

Use listView.setChoiceMode(int choiceMode);

Parameters

choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

(android.widget.AbsListView.MultiChoiceModeListener)

Refer to the sample below

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html

Zebadiah answered 19/12, 2011 at 19:0 Comment(1)
Thanks a lot! By adding a MultiChoiceModeListener and setting the background of the adapter in the xml file it worked! But I wonder why Android takes the background colour only when item is been selected..so automagicallyMicrolith
C
16

this is for custom listactivity or ListFragment

highlight selected item in ListView

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{

    for(int a = 0; a < parent.getChildCount(); a++)
    {
        parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
    }

    view.setBackgroundColor(Color.GREEN);
}
Cremator answered 27/9, 2013 at 9:41 Comment(0)
M
2

////@drawable/list_selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/list_item_bg_normal"
 android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>

<item android:drawable="@drawable/list_item_touch"
 android:state_pressed="true"/>

 <item android:drawable="@drawable/list_item_touch"
 android:state_pressed="false" android:state_selected="true"/>

 <item android:drawable="@drawable/list_item_bg_pressed"
 android:state_activated="true"/>

 </selector>

////////////////////and on ListView

 <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"
    android:divider="#060606"/>

///////////////////////listview Item

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/list_selector">

  <ImageView
  android:id="@+id/icon"
  android:layout_width="35dp"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_marginLeft="12dp"
  android:layout_marginRight="12dp"
  android:contentDescription="@string/desc_list_item_icon"
  android:src="@drawable/ic_home"
  android:layout_centerVertical="true" />

  </RelativeLayout>
Mechanotherapy answered 11/10, 2014 at 16:33 Comment(0)
S
1

The reason you are highlighting multiple items is probably because you are either: reusing the whole view, or setting the background of both of these views to the same Drawable instance. If you have the same drawable on the screen twice, all events that happen to the first will happen to all the others, because that logic is implemented in the instance of the Drawable itself.

To solve this, either: do not re-use Views for multiple rows, or do not re-use Drawables for multiple rows (create a new one each time)

I know this sounds resource intensive and it is, but unless you have a better solution figured out this is the easiest fix.

Sulfatize answered 19/12, 2011 at 18:58 Comment(1)
thanks man.. I am now trying to see where the code reuses the view, since I didn't wrote it. BTW, is it possible to let the selector stay on top as a highlight instead of changing the background? It makes more sense if it is doable.Microlith
B
0

Because the items in the listviews mimics the ones at the top, they also mimics the background of them. You have to set every one of the background of your items in getView() function. In every item in getView() you have to set the background for both selected ones and the unselected ones.

Bonis answered 28/10, 2012 at 4:46 Comment(0)
P
0

use this as list selector:

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

and this list_bg:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/list_bg_color" />
</shape>

choose the preferred highlight color

Psychogenic answered 1/6, 2015 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.