0

How would i get the link that the user clicks on in webview? Example: My webview take the user to a website that lists information and PDF's, if the user clicks on a PDF I would like to get the link of that PDF file the user clicked on and put it into Google's online viewer. The code I have doesn't work because it does not intercept that clicked on link. Any ideas?

Here is what I have:

webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // do your handling codes here, which url is the requested url
            // probably you need to open that url rather than redirect:
            if (url.startsWith("tel:")) {
                startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
            } else if (url.startsWith("mailto:")) {
                url = url.replaceFirst("mailto:", "");
                url = url.trim();
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL,
                        new String[] { url });
                startActivity(i);

            } else if (url.startsWith("geo:")) {
                try {
                } catch (Exception e) {
                    System.out.println(e);
                }

            } else if (url.endsWith("pdf")) {
                try{
                    String pdf = (url);
                    webview.loadUrl("http://docs.google.com/viewer?url=" + pdf);
                    }
                    catch (ActivityNotFoundException e)
                    {
                     Toast.makeText(atcFaa.this, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
                    }

            }

            else {
                view.loadUrl(url);
            }
            return true;
            // then it is not handled by default action
        }
    });
webview.loadUrl("http://www.somewebsite.com/publications/");
}
user1363871
  • 580
  • 3
  • 11
  • 29

1 Answers1

4

I think the method you want to override is WebViewClient.shouldInterceptRequest(WebView view, String url)

public WebResourceResponse shouldInterceptRequest (WebView view, String url)
  • Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used. NOTE: This method is called by the network thread so clients should exercise caution when accessing private data. Parameters

see webview shouldinterceptrequest example

Community
  • 1
  • 1
Akos Cz
  • 12,711
  • 1
  • 37
  • 32
  • I see that the minimum SDK version is 11, as said in that example. I am building for version 10 (for people still on 2.3.3 Gingerbread). Is my only option to upgrade to the next version, 11? – user1363871 May 09 '12 at 23:22