How to override web view text selection menu in android

Abhishek picture Abhishek · Sep 12, 2017 · Viewed 7.9k times · Source

The basic android's web text selection menu is as shown in image attached below. It has options like copy, share, select all, web search.

enter image description here

I want to over ride this menus and want them as my own menu list like "mark colour", "mark as imp" etc. I look around most of the questions available about context menu on stack overflow. The most of the question relate with context menu but not giving result as expected. I want menu like below image

enter image description here

When I perform selection android monitor shows some view creation form viewRoot like

D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{648898f V.E...... ......I. 0,0-0,0}
D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{a66541c V.E...... ......I. 0,0-0,0}
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1

How to achieve such implementation?

I also gone through https://github.com/naoak/WebViewMarker but not getting proper result.

What I have done yet?

I extend WebView of android and I want to make support for minimum SDK 19. When I perform long press I got long press event but I can't get such menus creation api calls.

Answer

Damodhar picture Damodhar · Sep 18, 2017

You need to overwrite action menus of activity

more info you can read :https://developer.android.com/guide/topics/ui/menus.html

HOW TO OVERWRITE:

@Override
public void onActionModeStarted(android.view.ActionMode mode) {
    mode.getMenu().clear();
    Menu menus = mode.getMenu();
    mode.getMenuInflater().inflate(R.menu.highlight,menus);
    super.onActionModeStarted(mode);
}

highlight

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/impclick"
        android:title="Mark As Important"
      />
    <item android:id="@+id/colorclick"
        android:title="Mark with color" />
</menu>