Kyle
Kyle

Reputation: 1639

WebView link click open default browser

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!

Upvotes: 134

Views: 151245

Answers (7)

ADasGH
ADasGH

Reputation: 511

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.

Upvotes: 0

Nikita Shavrin
Nikita Shavrin

Reputation: 163

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

Upvotes: 3

Cristiana Chavez
Cristiana Chavez

Reputation: 11529

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

Upvotes: 41

Amokrane Chentir
Amokrane Chentir

Reputation: 30395

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

Upvotes: 219

b1programmer
b1programmer

Reputation: 187

You only need to add the following line

yourWebViewName.setWebViewClient(new WebViewClient());

Check this for official documentation.

Upvotes: 13

BasavRaj
BasavRaj

Reputation: 111

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

Upvotes: 6

Piyush
Piyush

Reputation: 2609

you can use Intent for this:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);

Upvotes: 11

Related Questions