Question

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fine when I long press on Any item in the GridView. It is working fine. Now I have a requirement to activate the CAB menu when do press on a menu item in Action Bar. Once it is pressed, the CAB menu should read that 0 items are selected. After that it should allow me to select items from GridView on single clicks. How can I achieve this feature?

GridView set listener:

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;
        }

    });
Was it helpful?

Solution

From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your 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 simply start an ActionMode that you can manipulate for both situations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top