1

When selecting text in an android text view, a contextual action bar comes up with options to copy, cut, select all, share, etc. Is there a way to remove some of these options in my app?

thisiscrazy4
  • 1,905
  • 8
  • 35
  • 58

2 Answers2

3

You can inflate your own menu and then hide all the items that OS inserts.

First, keep track of all the IDs for your menu items:

List<Integer> mOptionsList = new ArrayList<Integer>();

/* put these two lines in onCreate() */
mOptionsList.add(R.id.my_option_1);
mOptionsList.add(R.id.my_option_2);

Then, hide any MenuItem that isn't yours in onPrepare:

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.my_contectual_menu, menu);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            if (!mOptionsList.contains(item.getItemId()))
                item.setVisible(false);
        }
        return false;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.my_option_1: {
                /* do something for option 1 */
                break;
            }
            case R.id.my_option_2: {
                /* do something for option 2 */
                break;
            }
            default:
                return false;
        }
    }

    public void onDestroyActionMode(ActionMode mode) {}
};
Krylez
  • 17,414
  • 4
  • 32
  • 41
  • What does my_contectual_menu contain? Wouldn't this just inflate our own menu without any of the system stuff? So what's the point of removing items in onPrepareActionMode? – thisiscrazy4 Aug 20 '13 at 21:46
  • That's the resource for your menu. By default, the OS inflates your menu, but it merges it with the system menu. That's why I'm hiding items that aren't mine. – Krylez Aug 20 '13 at 22:05
  • I created my own menu however the CAB only contains the items I added in the resource file. It doesn't have any of the system stuff. And that's without doing anything in the onPrepareActionMode function. – thisiscrazy4 Aug 20 '13 at 22:09
  • Any idea why it's not merging the system menu items? – thisiscrazy4 Aug 20 '13 at 22:21
  • this is the correct solution to guarantee to work in all devices. In my Z5 with Android 6.0 it was merging the System default menu items with my own menu, even if in onCreateActionMode I did the menu.clear(). Thanks @Krylez – mthandr Sep 11 '16 at 22:32
0

If you want to clear the default icons, You simply use menu.clear(). For instance altering the code above, we have private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    menu.clear;
    ...

    return true;
}

To remove a specific icon, you need to have the id of that icon. It would be something like menu.removeItem(android.R.id.copy) or something.

Samuel Agbede
  • 380
  • 1
  • 4
  • 14
  • Is it supposed to work in Android 11? I have tried it in emulator, but it did not remove the "intelligent" item, which I think the system adds when it thinks is necessary, for example, the "Map" menu when the selected text is an address, or the "Translate" menu when the selected text is a foreign language. – Damn Vegetables Jun 14 '21 at 14:24