Issue
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
Solution
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.
Answered By - naxrohan


0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.