I have an EditText, and I want the user to be able to select some text and apply some basic formatting to the selected text (bold, italics, etc.). However, I still need the standard copy, cut, and paste options. I read somewhere in the Android documentation, to do this, you have to call setCustomSelectionActionModeCallback () in EditText and pass ActionModeCallback () to it so that I do this. Here is my code:
In my activity, the onCreate () method:
myEditText.setCustomSelectionActionModeCallback(new TextSelectionActionMode());
Callback declaration:
private class TextSelectionActionMode implements ActionMode.Callback { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { menu.add("Bold"); return true; } @Override public void onDestroyActionMode(ActionMode mode) { } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } }
The problem I am facing is that when I click the overflow button (to access the Bold menu item), the ActionMode closes immediately. If I set it always as an action using this:
MenuItem bold = menu.add("Bold"); bold.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
It works fine, and I can click on it (although it obviously doesn't do anything). What am I missing here?
Edit: I just wanted to add that I was facing the same problem if I actually inflate the menu, rather than adding menu items programmatically. Once again, however, the problem disappears if I make it always show as an action.
mturco
source share