Skip to content Skip to sidebar Skip to footer

Highlight Multiple Selected/Checked/Activated In ListView

Wow, documentation is horrible when it comes to list item selection. All I need is to be able to select and highlight multiple items in a list. I've scoured the web and seen refere

Solution 1:

So apparently I was led off the wrong track because I was supposed to be looking for highlighting "checked" items and not "selected" items. Thus, many answers told me to use the selector with my ListView layout using android:listSelector="@drawable/myselector, but what I really needed was to use the selector with my row layout. The solution is actually rather simple, I'll post it below:

drawable/rowbackgroundselector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"
    android:drawable="@android:color/holo_green_light"/>
</selector>
  • note how you use "state_activated" to detect if an item is "checked"...

drawable/mylistrow.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:background="@drawable/rowbackgroundselector"
    android:padding="10sp"
/>
  • using the selector for the row background

MainActivity.onListItemClick()

public void onListItemClick(ListView l, View v, int position, long id) {
    getListView().setItemChecked(pos, true);
}

Lastly, make sure your adapter is using your custom row layout

mAdapter = new ArrayAdapter<FileTag>(this.getActivity(),
    R.layout.mylistrow, mList);

Post a Comment for "Highlight Multiple Selected/Checked/Activated In ListView"