Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open external links in the browser with android webview

I have this code, but not because it works, it keeps opening in webview and what I want is that the links do not belong to my website open in your default browser. Any idea? thanks

private class CustomWebViewClient extends WebViewClient {         @Override             public boolean shouldOverrideUrlLoading(WebView view, String url) {               if(url.contains("message2space.es.vu")){                 view.loadUrl(url);                 return true;             }else{                 return super.shouldOverrideUrlLoading(view, url);             }              }         } 
like image 886
Jaumesv Avatar asked Aug 01 '13 12:08

Jaumesv


People also ask

How do I open a link in WebView?

Within the shouldOverrideUrlLoading() method simply get the URL from the request and pass into the Intent. See the full example.


1 Answers

The problem is you need to send an Intent to the default web browser to open the link. What you are doing is just calling a different method in your Webview to handle the link. Whenever you want another app to handle something you need to use Intents. Try this code instead.

private class CustomWebViewClient extends WebViewClient {         @Override             public boolean shouldOverrideUrlLoading(WebView view, String url) {               if(url.contains("message2space.es.vu")) {                 view.loadUrl(url);               } else {                 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                 startActivity(i);               }               return true;             }         } 
like image 69
onit Avatar answered Oct 05 '22 22:10

onit