Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8515209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:05:10+00:00 2026-06-11T05:05:10+00:00

I try to catch webview longclicks to show a context menu. (see code below)

  • 0

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;
            }
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:05:12+00:00Added an answer on June 11, 2026 at 5:05 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Try Catch in my code. I would like to know if
I have never used Try-catch in my code before, but now I need to
Why is this code correct: try { } catch(ArrayOutOfBoundsException e) {} and this wrong:
I'm using the code below to load a text file content into a WebView
I'm using a try-catch block in the following Actionscript 3 code: try { this._subtitle
I have a webview that performs a search, this code works great for below
In my code i want to show webview and textview in a list from
In a sort of try/catch form I want to execute a bash that doesn't
Can you try/catch a stack overflow exception in java? It seems to be throwing
I have a try catch and finally block like this. Client client = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.