Highlight Listview Selected Row

How to highlight selected row in listview?

use android:listSelector and set color you want to Highlight

   <ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/popup_lstview_relations"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/relativeLayout"
android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray"/>

Highlight ListView selected row

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.

highlight selected row in listview

either add listView.requestFocusFromTouch() :

public void highlightItem(int position) {
listView.requestFocusFromTouch();
listView.setSelection(position);
}

or

Alternative solution is to call listView.setItemChecked(position, true); without requesting focus or setting selection, for this to work add

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

in your drowable/bg_key.xml

Important: : oh, and dont forget to set choice mode for you list, you need to do it once, i.e.:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Hope that helps

Highlighting the whole row when selected in ListView

If you use Details view you have to add some columns to your ListView control.

ListView1.Columns.Add("col1")
ListView1.Columns.Add("col2")

Don't forget to set ListView1.FullRowSelect = True, otherwise it won't select the whole row.

To auto size the columns width, set the width foreach column to -1.

For Each col As ColumnHeader In ListView1.Columns
col.Width = -1
Next

ListView Highlight selected items

You can do it by below code instead of applying in adapter class,
Check below code,

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

parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
//cardAdapter.setSelectedIndex(position);
}
});


Related Topics



Leave a reply



Submit