janoliver
janoliver

Reputation: 7824

Customize the ActionMenu of an EditText

I want to provide simple formatting buttons for an EditText, for example, a button that inserts <strong></strong> if no text was selected and wraps the selected text in <strong> tags otherwise.

I am using the Support Library v7 with ActionBarCompat. I managed to start a custom ActionMode upon focus change of my EditText. However, when I long-touch on text in that EditText, another ActionMode menu opens with copy/paste/... buttons, which presumably is Android's default behaviour. How can I suppress this second menu, but still let the user select portions of the EditText's content?

Alternatively I would like to be able to customize the default menu using setCustomSelectionActionModeCallback(...) but using the Support library. How could I do that?

Upvotes: 4

Views: 1232

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

Unfortunately, TextView and EditText don't provide an API for you to use ActionBarCompat classes. But, you should be able to override this behavior.

Set a custom onLongClickListener in which you set your own state information in your activity and call supportInvalidateOptionsMenu(). Be sure to return 'true' from your OnLongClickListener.onLongClick() method, indicating you consumed the click. Then, in your onPrepareOptionsMenu you can add whatever new menu items you wish for your custom action bar. I would recommend not using TextView context menu items IDs 'selectAll', 'cut', 'copy' and 'paste', just to be on the safe side.

Upvotes: 3

Related Questions