In this tutorial, we will learn how to respond when a user selects multiple list items in an Activity . For the purpose of this tutorial, I will be selecting multiple list items for deleting them from the database. This tutorial also covers on how to change listItem background color when the user selects multiple items.
Idea :
Listen to long press on a list item and store list id in an ArrayList if checked state is True, otherwise, if checked state is false then remove the item id from ArrayList.
pre-setup :
If you are displaying list using values from the database, then make sure that the primary key name is "_id" in your database table.
Now follow below-given steps to listen for multiple list item selection
1. Set Global Variable
3. Set MultiChoiceModeListener to your ListView variable
Idea :
Listen to long press on a list item and store list id in an ArrayList if checked state is True, otherwise, if checked state is false then remove the item id from ArrayList.
pre-setup :
Added a ListView to the layout file and attached a cursorAdepter with it.This how my final
listview looks like.
If you are displaying list using values from the database, then make sure that the primary key name is "_id" in your database table.
Now follow below-given steps to listen for multiple list item selection
1. Set Global Variable
Context context; private ListView list; private ArrayList2. Change list CHOICE_MODE as followslist_item = new ArrayList<>(); // for holding list item ids
//(list) is my ListView list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); // Important
3. Set MultiChoiceModeListener to your ListView variable
list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { //capture total checked items checkedCount = list.getCheckedItemCount(); //setting CAB title mode.setTitle(checkedCount + " Selected"); //list_item.add(id); if(checked){ list_item.add(id); // Add to list when checked == true }else { list_item.remove(id); } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { //Inflate the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.contextual_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { final int deleteSize = list_item.size(); int itemId = item.getItemId(); if(itemId == R.id.item_delete) { for(long ids : list_item){ // Make proper check, if needed, before deletion String whereDelId = DBOpenHelper.COL_ID + "=" + ids; int res = getContentResolver().delete(TripProvider.CONTENT_URI_START, whereDelId, null); if(res == -1){ Log.d(TAG, "onActionItemClicked: Delete Failed for ID = "+ids); } } } Toast.makeText(context,deleteSize+" Items deleted",Toast.LENGTH_SHORT).show(); checkedCount = 0; list_item.clear(); mode.finish(); return true; } @Override public void onDestroyActionMode(ActionMode mode) { // refresh list after deletion displayDataList(); } });
Changing background Color of Selected ListView List items.
4. Create a drawable XML for list item background.{mine is, select_list_bg,xml}<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime"> <item android:state_pressed="true" android:state_activated="true" android:drawable="@color/lightPink"></item> <item android:state_activated="true" android:drawable="@color/lightPink"></item> <item android:drawable="@color/lightGreen"></item> <item android:state_enabled="true" android:drawable="@color/lightGreen"></item> </selector>5. Set above drawable as background to listItem
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_list_bg,xml"
android:elevation="4dp">
<!-- Your List item child views -->
</RelativeLayout>
This is the final look of the List when user selects multiple items.