Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight ListView selected row

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>

Quoted from http://android-developers.blogspot.de/2008/12/touch-mode.html

Imagine a simple application, ApiDemos for example, that shows a list of text items. The user can freely navigate through the list using the trackball but also, alternatively, scroll and fling the list using the touch screen. The issue in this scenario is how to handle the selection properly when the user manipulates the list through the touch screen.

In this case, if the user selects an item at the top of the list and then flings the list towards the bottom, what should happen to the selection? Should it remain on the item and scroll off the screen? What should happen if the user then decided to move the selection with the trackball? Or worse, what should happen if the user presses the trackball to act upon the currently selected item, which is not shown on screen anymore?

After careful consideration, we decided to remove the selection altogether, when the user manipulates the UI through the touch screen.

In touch mode, there is no focus and no selection. Any selected item in a list of in a grid becomes unselected as soon as the user enters touch mode. Similarly, any focused widgets become unfocused when the user enters touch mode. The image below illustrates what happens when the user touches a list after selecting an item with the trackball.


Maaalte is correct, in touch mode lists do not show the focus highlight on a current item since there is no current item in touch mode. Stateful selection like you're describing that persists while you interact with other parts of the UI is another matter though.

You can use CHOICE_MODE_SINGLE to express a stateful selection in a list. It will treat selection as a checked state. As long as the view that your adapter returns implements the Checkable interface, you can display the selected state however you choose. (Setting a background on the item view or otherwise. It does not actually need to show a check box widget.) You can then use the item checked methods of ListView to manipulate or determine the selection.


I solved this by extending the ListView and overriding the getView() method. I kept an internal identifier for the "selected" state and changed the background of the item according to it, like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currView = super.getView(position, convertView, parent);
StateListItem currItem = getItem(position);
if (currItem.isItemSelected) {
    currView.setBackgroundColor(Color.RED);
} else {
    currView.setBackgroundColor(Color.BLACK);
}
return currView;
}  

I have an example program on my blog post: http://udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/


Here is my solution Listview:

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:choiceMode="singleChoice"
        android:paddingTop="8dip" >
    </ListView>

Write a selector for list_row as

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

Make sure you have item for state_activated=true

Then add this selector as your list_row background

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

<TextView
    android:id="@+id/itemName"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


add android:background="?activatedBackgroundIndicator
to list item's main layout similar to android.R.layout.simple_list_item_activated_2.xml


Implement your list with single choice mode: android:choiceMode=singleChoice

Just as the default android.R.layout.simple_list_item_single_choice uses a RadioButton to denote the selected choice, you can implement a custom highlight state or similar in your list item layout to respond to the choice changes. Methods like getCheckedItemPosition() can be used by your application to determine which item the user currently has chosen, if necessary.

Hope that Helps!