Activate CAB menu when OnClickEvent happens in Android

Trying to activate the CAB menu by clicking on MenuItem from the ActionBar. This is how I installed GridView to listen to Multi Choice. MultiModeChoiceListener works fine when I click on Any item in the GridView for a long time. It is working fine. Now I have a requirement to activate the CAB menu when you click on a menu item in the action bar. After pressing it, the CAB menu should indicate that 0 items have been selected. After that, he should let me select elements from the GridView with one click. How can I achieve this feature?

GridView Set Receiver:

gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); gv.setMultiChoiceModeListener(new MultiChoiceModeListener()); 

MultiChoiceModeListener.java

 public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener { public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu); MenuItem mi = menu.findItem(R.id.close); mi.setIcon(R.drawable.cancel); mode.setTitle("Select Items"); return true; } public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return true; } public boolean onActionItemClicked(ActionMode mode, MenuItem item) { Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show(); if (item.getTitle().toString().equalsIgnoreCase("Close")) { mode.finish(); } return true; } public void onDestroyActionMode(ActionMode mode) { new ChangeNotifier().changeOnFavoriteStore = true; new AddFavorites().execute("add", device_id, dataArray); if (notify == true) { Toast.makeText(getApplicationContext(), "Selected items are added to Favorites", Toast.LENGTH_SHORT).show(); notify = false; } } public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { int selectCount = gridView.getCheckedItemCount(); if (selectCount > 0) { notify = true; dataArray.add(position); switch (selectCount) { case 1: mode.setSubtitle("One item added to favorites"); break; default: mode.setSubtitle("" + selectCount + " items added to favorites"); break; } } } 

OnMenuItemClick Method:

  public boolean onPrepareOptionsMenu(final Menu menu) { final MenuItem editItem = menu.findItem(R.id.editit); editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { //the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar return false; } }); 
+4
source share
1 answer

From your question, I understand that you are trying to start CAB with a GridView by clicking on one of the menu items. I don’t know if you can do this (but I could be wrong), since the MultiChoiceModeListener expects the item to be checked to run. Depending on your layout and the overall appearance of the GridView , I think you could have a dummy element (as an optional element in the adapter) at the end of the GridView (without content) and use setItemChecked(dummyItemPosition, true) to launch the CAB GridView . Of course, you will need additional logic to take care of this additional element in the MultiChoiceModeListener :

  public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { if (position == theDummyPosition) return; // so we start the CAB but there aren't any items checked } int selectCount = gridView.getCheckedItemCount(); if (selectCount > 0) { notify = true; dataArray.add(position); // if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it switch (selectCount) { case 1: mode.setSubtitle("One item added to favorites"); break; default: mode.setSubtitle("" + selectCount + " items added to favorites"); break; } } } 

The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and just run an ActionMode that you can manipulate for both situations.

+4
source

All Articles