56

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

            }
        }
Jaumesv
  • 1,065
  • 3
  • 11
  • 19

4 Answers4

105

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.

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    final String url = request.getUrl().toString();
    if (url.contains("message2space.es.vu")) {
        view.loadUrl(url);
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
        startActivity(intent);
    }
    return true;
}
kalucki23
  • 172
  • 4
  • 15
onit
  • 6,306
  • 3
  • 24
  • 31
30

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;
        }
    });
Hativ
  • 1,500
  • 1
  • 16
  • 24
  • great, this works like charm. This is really simple since it doesn't involve overwriting `WebView`. Maybe @Jaumesv can make it the new excepted answer to pin it clearly to the top... – sunadorer Aug 07 '18 at 12:09
  • 4
    Request.getUrl() has min SDK 21 requirements. – Sergio Sep 23 '19 at 08:49
10
 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;
            }
        });
Siber Medya
  • 101
  • 1
  • 3
6

Here is very sweet and short solution

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    context.startActivity(i);
    return true;
}
Anand Savjani
  • 2,484
  • 3
  • 26
  • 39