4

I have been trying to customize the contextual menu for a webview, that appears when selecting a text in the webview.

Basically I want to add or remove items to the menu. And add some custom event on item click.

I have already tried few solution like below links, with no success. All I end up getting is a Dialogue menu.

https://developer.android.com/guide/topics/ui/menus.html#CAB

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
naxrohan
  • 352
  • 3
  • 15
  • 1
    Possible duplicate? https://stackoverflow.com/questions/23737510/webview-add-menu-item-to-textselection-menu – ozbek Jun 04 '19 at 06:25

1 Answers1

0

After much trial and error, I have come to a reasonable solution. Just posting it here for completeness, just in case anyone faces similar issue.

The solution was creating an alternative menu that I implemented using PopupWindow. And I, called this on long press event using a GestureDetector.

myWebView.setWebChromeClient(new MyWebChromeClient());
mGestureDetector = new GestureDetector(getActivity(), new CustomGestureListener());
mGestureDetector = new GestureListener(getActivity(),myWebView);
final GestureDetector gd = new GestureDetector(getActivity(), mGestureDetector);

    //====== web-view popup menu
    myWebView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            gd.onTouchEvent(motionEvent);

            return false;
        }
    });

The Gesture Listener is quite basic, code can be found here

Rest of the interaction with the web-view data is done using JavaScript, using below code, more about it can be found in google dev guide

WebView xview = (WebView)view;
xview.loadUrl("javascript:alert(showAndroidToast('underline'))");

Well this has done the job for me, result can be seen in below image, hope it helps someone.

Or unless someone has a better solution/code.

enter image description here

naxrohan
  • 352
  • 3
  • 15