4

I am trying to present a custom action bar while long pressing a textview. My menu has more than 5 items which causes some of the items to be present under the overflow menu.

When I press the overflow icon, the action bar gets destroyed and I am not able to choose any item inside the overflow.

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

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.add_rule_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;
        }

        // Clicking on overflow button does not trigger this method at all.
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
             // Rest of the code
            }
        }

        public void onDestroyActionMode(ActionMode mode) {}
    };

    textView.setCustomSelectionActionModeCallback(mActionModeCallback);
Mahadevan Sreenivasan
  • 1,144
  • 1
  • 9
  • 26

1 Answers1

5

I filed an issue about this years ago, which has never been resolved.

A cheesy workaround is to use nested action modes. By this, I mean you have an item in an action mode that finishes the current mode and starts a new one, to provide a "drill-down menu" effect. I use this in my recently-resuscitated RichEditText widget, which offers an action mode for formatting text. I add a "format" item to the default action mode via setCustomSelectionActionModeCallback(). Tapping on "format" opens another action mode that offers options like bold and italic, along with further drill-downs to get to thinks like font changes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks for this. I might have to do somekind of workaround as you implemented. After coming back from a long hiatus in Android development, I still see that these kind of naive bugs are everywhere in the SDK. – Mahadevan Sreenivasan May 17 '14 at 14:38