How to Highlight Row in Listview in Android

How to highlight row in ListView in Android?

Since by default ListViews are set to a selection mode of NONE, in touch mode the setSelection method won't have visual effect.

For keeping the previous selection / visually display an explicit selection, first you must set your listview's choice mode appropriately:

listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

It's useful to read the API Docs of these methods:

  • setSelection
void android.widget.AdapterView.setSelection(int position)

Sets the currently selected item. To
support accessibility subclasses that
override this method must invoke the
overriden super method first.

Parameters:

position Index (starting at 0) of the data item to be selected.

  • setChoiceMode
void android.widget.ListView.setChoiceMode(int choiceMode)

Defines the choice behavior for the
List. By default, Lists do not have
any choice behavior
(CHOICE_MODE_NONE). By setting the
choiceMode to CHOICE_MODE_SINGLE, the
List allows up to one item to be in a
chosen state. By setting the
choiceMode to CHOICE_MODE_MULTIPLE,
the list allows any number of items to
be chosen.

Parameters:
choiceMode One of CHOICE_MODE_NONE,
CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE

In case this is not enough (say you'd like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the ListAdapter) as lastSelectedItem, and in your adapter's getView method assign a different background resource to the renderer if it equals this lastSelectedItem.

If your last selection wouldn't refresh on selection change, you should explicitly call the notifyDataSetChanged method on your adapter instance.

Update

Since your activity containing the ListView is a child of an activity which waits for this one's result (based on the setResult(Activity.RESULT_OK,pongIntent); part), the initial idea is correct, the last position should be passed through the intent when starting the activity:

selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);

The ListView.CHOICE_MODE_SINGLE solution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that's why the extra data should be passed to the starting Intent.

You can also set the previously selected item's background from your adapter -as mentioned above-, overriding its getView method:

lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (position == selectedListItem)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}
return renderer;
}
});

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"/>

How to Highlight single item in Listview

add boolean flag in your pojo class say

Boolean isSelected;

apply getter and setters in it.
initially set all the values for isSelected = false so that no list item is selected

now during onItem click set the current Object's isSelected to true
like this

within the onItemClick apply this

OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {

for(int i =0;i < lstcategory.size();i++}{
lstcategory.get(i).setIsSelected(false);
}

lstcategory.get(position).setIsSelected(true);
mAdapter.notifyDataSetChanged();

}
};

now in getview of your adapter class check if the lists item isChecked or not.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);

if (lstcategory.get(position).getIsSelected()) {
// set selected your color
}else{
//set default color
}

return view;
}

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



Related Topics



Leave a reply



Submit