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.
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);
Author by
Kyle
Updated on February 18, 2021Comments
-
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 almost 13 yearsyou should use Intent.ACTION_VIEW
-
Android-Droid over 12 yearsthis answer helped me a lot! Thanks!
-
Johan S almost 11 yearsNote 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 about 10 yearsYou 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 about 8 yearsExact, what I am searching. Thanks
-
Atieh about 8 yearshey, 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 about 8 yearsremoving it did not help.
-
user2899094 over 7 yearsthe 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 almost 7 yearsNote that
shouldOverrideUrlLoading(WebView view, String url)
is deprecated in API 24. Check this answer. -
Arslan Ali over 4 yearsSaved my life. Thank youuu
-
Jonas Erbe over 3 yearsFrom 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.