Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get link-URL in Android WebView with HitTestResult for a linked image (and not the image-URL) with Longclick

I try to catch webview longclicks to show a context menu. (see code below) When longclicking an image, I always get the image-URL as extra (for a not linked image with IMAGE_TYPE and for a linked image with SRC_IMAGE_ANCHOR_TYPE). But how can I get the Link-URL (and not the image-URL) for an image with a hyperlink?

Best, Sebastian

        mywebview.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {

                final WebView webview = (WebView) v;
                final WebView.HitTestResult result = webview.getHitTestResult();

                if (result.getType() == SRC_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == SRC_IMAGE_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == IMAGE_TYPE) {
                    return true;
                }

                return false;
            }
        });
like image 611
Sebastian Avatar asked Aug 28 '12 21:08

Sebastian


1 Answers

I checked the source code of the WebView and it seems that the image uri is the only extra data you can get for SRC_IMAGE_ANCHOR_TYPE. But don't be mad here I have a quick and dirty workaround for you:

    webview.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            final WebView webview = (WebView) v;
            final HitTestResult result = webview.getHitTestResult();
            if(result.getType()==HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                webview.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // 2. and here we get the url (remember to remove the WebView client and return true so that the hyperlink will not be really triggered)
                        mUrl = url; // mUrl is a member variant of the activity
                        view.setWebViewClient(null);
                        return true;
                    }
                });
                // 1. the picture must be focused, so we simulate a DPAD enter event to trigger the hyperlink
                KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event1);
                KeyEvent event2 = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event2);
                // 3. now you can do something with the anchor url (and then clear the mUrl for future usage)
                String url = mUrl;
                if (url!=null) {
                    Toast.makeText(webview.getContext(), url, Toast.LENGTH_SHORT).show();
                }

                mUrl = null;
            }
            return false;
        }
    });

I tried the code on a low-end Android 2.1 device and a high-end Android 4.0 device, both work like a charm.

Regards

Ziteng Chen

like image 56
Ziteng Chen Avatar answered Oct 19 '22 23:10

Ziteng Chen