Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight selected item in ListView?

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!

like image 805
Han Avatar asked Dec 19 '11 18:12

Han


3 Answers

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

like image 178
Rajdeep Dua Avatar answered Nov 11 '22 01:11

Rajdeep Dua


////@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>
like image 43
shakil.k Avatar answered Nov 10 '22 23:11

shakil.k


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);
}
like image 16
sudam Avatar answered Nov 11 '22 01:11

sudam