Skip to content Skip to sidebar Skip to footer

Horizontal Menu Inflater On Long Click For Web View

I am having a problem with the webview selection on longClick. I already had an implementation of a customized menu that launches on longClick. But the default menu is launching as

Solution 1:

In whichever activity is hosting your WebView, override onActionModeStarted(), manipulate the menu items, and assign listeners to each. An example:

@OverridepublicvoidonActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);

    MenuInflatermenuInflater= mode.getMenuInflater();
    Menumenu= mode.getMenu();

    menu.clear();
    menuInflater.inflate(R.menu.menu_custom, menu);

    menu.findItem(R.id.custom_one).setOnMenuItemClickListener(newToastMenuItemListener(this, mode, "One!"));
    menu.findItem(R.id.custom_two).setOnMenuItemClickListener(newToastMenuItemListener(this, mode, "Two!"));
    menu.findItem(R.id.custom_three).setOnMenuItemClickListener(newToastMenuItemListener(this, mode, "Three!"));
}

privatestaticclassToastMenuItemListenerimplementsMenuItem.OnMenuItemClickListener {

    privatefinal Context context;
    privatefinal ActionMode actionMode;
    privatefinal String text;

    privateToastMenuItemListener(Context context, ActionMode actionMode, String text) {
        this.context = context;
        this.actionMode = actionMode;
        this.text = text;
    }

    @OverridepublicbooleanonMenuItemClick(MenuItem item) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        actionMode.finish();
        returntrue;
    }
}

Solution 2:

This is the solution that worked for me, hope it may be useful for someone.

The only difference from the previous answer is that I created the menu dynamically, cause the static menu for some reason was not responding to menuItemClicked() function.

@OverridepublicvoidonActionModeStarted(ActionMode mode) {
    System.out.println("onActionModeStarted");
    if (mActionMode == null)
    {
        mActionMode = mode;
        //mode.setTitle("Dictionary");Menumenu= mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.menu, menu);
        List<MenuItem> menuItems = newArrayList<>();
        // get custom menu itemfor (inti=0; i < menu.size(); i++) {
            menuItems.add(menu.getItem(i));
        }
        menu.clear();
        // reset menu item orderintsize= menuItems.size();
        for (inti=0; i < size; i++) {
            addMenuItem(menu, menuItems.get(i), i, true);
        }

    }

    //System.out.println("onActionModeStarted");super.onActionModeStarted(mode);
}


privatevoidaddMenuItem(Menu menu, MenuItem item, int order, boolean isClick){
    finalMenuItemmenuItem= 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(newOnMenuItemClickListener() {

            @OverridepublicbooleanonMenuItemClick(MenuItem item) {

                BookReader.this.menuItemClicked(item);
                returntrue;
            }
        });
}

publicvoidmenuItemClicked(MenuItem item)
{
    System.out.println("menuItemClicked");

    switch (item.getItemId())
    {
        case R.id.dict_menu:
            testWV.searchInDict();
            if (mActionMode!=null){
                mActionMode.finish();
            }

            break;
        case R.id.hi_menu:
            testWV.highlightWord();

            break;
        case R.id.q_menu:
            testWV.copyQuote();
            if (mActionMode!=null){
                mActionMode.finish();
            }
            break;
    }
}

Post a Comment for "Horizontal Menu Inflater On Long Click For Web View"