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/");
}