WebView link click open default browser
Asked Answered
T

7

134

Right now I have an app that loads a webview and all the clicks are kept within the app. What I would like to do is when a certain link, for example, http://www.google.com is clicked within the app it opens the default browser. If anyone has some ideas please let me know!

Termitarium answered 19/11, 2010 at 21:23 Comment(0)
P
217

I had to do the same thing today and I have found a very useful answer on StackOverflow that I want to share here in case someone else needs it.

Source (from sven)

webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});
Paucity answered 14/6, 2011 at 13:1 Comment(7)
Note that if the url is relative, (doesn't start with "http://") then it will open inside the app. To avoid this always return true and make relative url links do nothing.Thrush
You should check for other protocols in the prefix like rtsp, https and so on. If the links are intended to open a media, it should be redirected to device's media player. If there is no protocol prefix, then identify and provide one.Eslinger
hey, thanks for this. I have a problem, when I go back from my launched activity, the calling one that had the webview would be finished? How can I make sure the activity containing the webview remain open on back navigation? EDIT: nvm, I had the noHistory set to true, thanks!Dauphine
removing it did not help.Dauphine
the problem with this is that when i change the URL using loadURL during runtime, the shouldOverrideUrlLoading function/method is triggered. But we dont want this, we only want clicks to open externally, so there need to be a check to see if touchedSyconium
Note that shouldOverrideUrlLoading(WebView view, String url) is deprecated in API 24. Check this answer.Moron
From the security perspective it is better to check which url's SHOULD open IN the webview and ALL OTHERS should open in the default browser.Problem
P
41
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl(https://whatoplay.com/);

You don't have to include this code.

// webview.setWebViewClient(new WebViewClient());

Instead use below code.

webview.setWebViewClient(new WebViewClient()
{
  public boolean shouldOverrideUrlLoading(WebView view, String url)
  {
    String url2="https://whatoplay.com/";
     // all links  with in ur site will be open inside the webview 
     //links that start ur domain example(http://www.example.com/)
    if (url != null && url.startsWith(url2)){
      return false;
    } 
     // all links that points outside the site will be open in a normal android browser
    else
    {
      view.getContext().startActivity(
      new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      return true;
    }
  }
});
Panteutonism answered 25/9, 2013 at 8:32 Comment(0)
L
14

You only need to add the following line

yourWebViewName.setWebViewClient(new WebViewClient());

Check this for official documentation.

Lorenzo answered 9/3, 2015 at 0:18 Comment(0)
D
11

you can use Intent for this:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);
Diatomite answered 20/11, 2010 at 4:42 Comment(1)
you should use Intent.ACTION_VIEWIniquity
A
6

You can use an Intent for this:

Uri uriUrl = Uri.parse("http://www.google.com/"); 
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
startActivity(launchBrowser);  
Admittance answered 10/5, 2012 at 6:44 Comment(0)
C
3

As this is one of the top questions about external redirect in WebView, here is a "modern" solution on Kotlin:

webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            val url = request?.url ?: return false
            //you can do checks here e.g. url.host equals to target one
            startActivity(Intent(Intent.ACTION_VIEW, url))
            return true
        }
    }
Cornish answered 18/2, 2021 at 8:23 Comment(0)
L
0

Now that public boolean shouldOverrideUrlLoading(WebView view, String url) is deprecated and public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) is used instead, you can use the following code in Java:

private class MyWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (Objects.equals(request.getUrl().toString(), "http://www.google.com/")) {
                Uri uri = request.getUrl();
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
                return true;
            }
            else {
                return false;
            }
        }
    }

Just replace http://www.google.com/ with the URL you want to open in web browser instead of webview.

Lime answered 18/8, 2023 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.