127

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!

Swati Garg
  • 901
  • 1
  • 10
  • 21
Kyle
  • 1,549
  • 3
  • 12
  • 11

6 Answers6

212

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;
        }
    }
});
Linh
  • 51,033
  • 19
  • 228
  • 256
Amokrane Chentir
  • 29,280
  • 37
  • 112
  • 157
  • 3
    this answer helped me a lot! Thanks! – Android-Droid Sep 14 '11 at 07:24
  • 8
    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. – Johan S Jun 04 '13 at 10:48
  • 3
    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. – Abhinav Saxena Feb 20 '14 at 06:10
  • Exact, what I am searching. Thanks – Sudarshan Feb 28 '16 at 06:58
  • 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! – Atieh Apr 21 '16 at 23:18
  • removing it did not help. – Atieh Apr 21 '16 at 23:28
  • 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 touched – user2899094 Sep 09 '16 at 23:58
  • 4
    Note that `shouldOverrideUrlLoading(WebView view, String url)` is deprecated in API 24. Check [this answer](http://stackoverflow.com/a/38484061/3752244). – Mateus Gondim May 13 '17 at 19:26
  • Saved my life. Thank youuu – Arslan Ali Nov 13 '19 at 14:34
  • 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. – Jonas Erbe Dec 02 '20 at 10:48
39
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;
    }
  }
});
Mete
  • 2,767
  • 1
  • 25
  • 37
Cristiana Chavez
  • 11,231
  • 5
  • 54
  • 53
14

You only need to add the following line

yourWebViewName.setWebViewClient(new WebViewClient());

Check this for official documentation.

Ram
  • 3,032
  • 10
  • 39
  • 56
b1programmer
  • 189
  • 1
  • 5
11

you can use Intent for this:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);
Amokrane Chentir
  • 29,280
  • 37
  • 112
  • 157
Piyush
  • 2,539
  • 6
  • 37
  • 77
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);  
Perception
  • 77,470
  • 19
  • 176
  • 187
BasavRaj
  • 101
  • 1
  • 2
2

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