Overflow button enforces action mode

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.

+7
source share
3 answers

This is a skeleton issue. If the textview receives a "focused change" event, the text window stops the action. When a popup popup is displayed, focus with no text.

+3
source

This issue has been resolved in Android 6.0. However, you must use ActionMode.Callback2 as described here in Android 6.0.

For Android 5.x and below, I recommend this workaround: add a Toolbar or ActionBar button that records the current selection and then opens another context menu.

 this.inputText_selectionStart = inputText.getSelectionStart(); this.inputText_selectionEnd = inputText.getSelectionEnd(); registerForContextMenu(inputText); openContextMenu(inputText); unregisterForContextMenu(inputText); 
+1
source

This is a registered Android bug: https://code.google.com/p/android/issues/detail?id=82640 . This link contains a workaround. Fortunately, this has been fixed in Android 6.0.

0
source

All Articles