Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Mode modify the action menu dynamically

I extend AbsListView.MultiChoiceModeListener for multi selection in ListView, I want to change the action menu dynamically (when more than one ListView item selected).

private class ModeCallback implements ListView.MultiChoiceModeListener {
        //inflate menu 
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.compose_multi_select_menu, menu);
        }
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            //want to remove some menu here, but not work
            if (getListView().getCheckedItemCount() > 1) {
            MenuItem item = menu.getItem(5);
            menu.removeItem();
            }
        }

}

I try to remove MenuItem in onPrepareActionMode(), but not work. Also tried mode.invalidate() in onItemCheckedStateChanged().

Actually, I find in onPrepareActionMode() the menu passed in have no MenuItem at all.

Anyone can help on this ?

like image 708
zhibin Avatar asked Jan 31 '26 14:01

zhibin


1 Answers

You can modify menu in your onItemCheckedStateChanged() by showing or hiding items like this:

Menu menu = mode.getMenu();
menu.findItem(R.id.some_item_id).setVisible(false);

where mode is ActionMode passed to onItemCheckedStateChanged

like image 52
Vladimir Petrakovich Avatar answered Feb 02 '26 07:02

Vladimir Petrakovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!