6

I have a requirement like when i long press on the text in my web view by clicking long press, i should set my custom context menu items instead of "select", "select all", "web search".

Please help me.

enter image description here

Would like to override these default "select all", "copy", "share", "web search". in this place wanna place my custom menus.

Suresh
  • 1,199
  • 2
  • 12
  • 36
  • Take a look at [this](http://stackoverflow.com/questions/22336903/use-a-custom-contextual-action-bar-for-webview-text-selection) question . – Roozbeh Oct 14 '15 at 22:24
  • May be this answer can help you: [How to override default text selection of android webview](http://stackoverflow.com/a/22563790/2722270), and this [Use a custom contextual action bar for WebView text selection](http://stackoverflow.com/q/22336903/2722270) – Weiyi Oct 20 '15 at 10:39

2 Answers2

3

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

See Use a custom contextual action bar for WebView text selection

Community
  • 1
  • 1
Fernando Martínez
  • 1,057
  • 10
  • 13
3

you can do some custom in 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);
}
OriginQiu
  • 31
  • 4