WebView link click open default browser

123,123

Solution 1

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

Solution 2

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

Solution 3

You only need to add the following line

yourWebViewName.setWebViewClient(new WebViewClient());

Check this for official documentation.

Solution 4

you can use Intent for this:

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

Solution 5

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);  
Share:
123,123
Kyle
Author by

Kyle

Updated on February 18, 2021

Comments

  • Kyle
    Kyle about 3 years

    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!

  • rajh2504
    rajh2504 almost 13 years
    you should use Intent.ACTION_VIEW
  • Android-Droid
    Android-Droid over 12 years
    this answer helped me a lot! Thanks!
  • Johan S
    Johan S almost 11 years
    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.
  • Abhinav Saxena
    Abhinav Saxena about 10 years
    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.
  • Sudarshan
    Sudarshan about 8 years
    Exact, what I am searching. Thanks
  • Atieh
    Atieh about 8 years
    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
    Atieh about 8 years
    removing it did not help.
  • user2899094
    user2899094 over 7 years
    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
  • Mateus Gondim
    Mateus Gondim almost 7 years
    Note that shouldOverrideUrlLoading(WebView view, String url) is deprecated in API 24. Check this answer.
  • Arslan Ali
    Arslan Ali over 4 years
    Saved my life. Thank youuu
  • Jonas Erbe
    Jonas Erbe over 3 years
    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.