WebView link click open default browser
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;
}
}
});
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;
}
}
});
You only need to add the following line
yourWebViewName.setWebViewClient(new WebViewClient());
Check this for official documentation.
you can use Intent for this:
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);