Open external links in the browser with android webview Open external links in the browser with android webview android android

Open external links in the browser with android webview


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;            }        }


Since API level 24 shouldOverrideUrlLoading(WebView view, String url) is deprecated.

Up to date solution:

    webView.setWebViewClient(new WebViewClient() {        @Override        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {            Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());            view.getContext().startActivity(intent);            return true;        }    });


 webView.setWebViewClient(new WebViewClient()   {            @Override            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {                if((String.valueOf(request.getUrl())).contains("paramedya.com.tr")) {                    view.loadUrl(String.valueOf(request.getUrl()));                } else {                    Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());                    view.getContext().startActivity(intent);                }                return true;            }        });