How to override webview text selection context menu?

I have a requirement, for example, when I click on the text in my web view for a long time, pressing the long press, I have to set my custom context menu items instead of "select", "select all", "web search".

Please help me.

enter image description here

You would like to override these defaults: "select all", "copy", "share", "web search". in this place you want to place your custom menus.

+5
source share
2 answers

Unfortunately, you need to extend the WebView class and override the onCreateContextMenu method.

See Using a custom contextual action bar to select WebView text.

+3
source

you can make some settings in the activity method: onActionModeStarted (ActionMode mode), just like this:

 @Override public void onActionModeStarted(ActionMode mode) { if (mActionMode == null) { mActionMode = mode; Menu menu = mode.getMenu(); menu.clear(); getMenuInflater().inflate(R.menu.YOUR_MENU, menu); List<MenuItem> menuItems = new ArrayList<>(); // get custom menu item for (int i = 0; i < menu.size(); i++) { menuItems.add(menu.getItem(i)); } menu.clear(); // reset menu item order int size = menuItems.size(); for (int i = 0; i < size; i++) { addMenuItem(menu, menuItems.get(i), i, true); } super.onActionModeStarted(mode); } } /** * add custom item to menu * @param menu * @param item * @param order * @param isClick */ private void addMenuItem(Menu menu, MenuItem item, int order, boolean isClick){ MenuItem menuItem = menu.add(item.getGroupId(), item.getItemId(), order, item.getTitle()); menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); if (isClick) // set custom menu item click menuItem.setOnMenuItemClickListener(this); } 
+3
source

All Articles