w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Hide Items in Context Action Bar (CAB) dynamically when multiple items are selected

As suggested in adneal's comment.

Add invalidate() to onItemCHeckedStateChanged()

@Override public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
     if (checked) {
         selCount++;
         idList.add(id);
     } else {
         selCount--;
         idList.remove(id);
     }
     mode.setTitle(selCount + " selected");

     mode.invalidate();  // Add this to Invalidate CAB
}

This invalidates the CAB and causes the onPrepareActionMode() function to be called.

@Override public boolean onPrepareActionMode(ActionMode mode,
Menu menu) {
   if (selCount == 1){
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(true);
       return true;
   } else {
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(false);
       return true;
   }
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.