Is it time to charge your devices.?
Android Webview open link in browser, if you are using an android Webview in your app but would like to open some type of URL’s in your phones default browser like Chrome or Firefox, here is what you need to do.
Android Webview open link in browser code snippet
You need to override WebViewClient’s shouldOverrideUrlLoading
like so:
myWebWiev = (WebView)findViewById(R.id.myWebWiev);
myWebWiev.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// open in Webview
if (url.contains("android_asset") ){
// Can be clever about it like so where myshost is defined in your strings file
// if (Uri.parse(url).getHost().equals(getString(R.string.myhost)))
return false;
}
// open rest of URLS in default browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
});
For more Android related posts check out my Android category page.
Have fun! and please Like♥ the page if you found this useful!
Please feel free to add your own solutions in the comments!
Photo credit to etnyk at Flickr.
Cheers!
thank you so much. Solved my problem
Samuel,
Thank you so much for your comment, I am really glad I was able to help you!
Cheers!
Lehel
Thanks for sharing. I’m starting in this world of app and this help me a lot. Regards!
Hi Professor Yeow,
Thank you for your comment!
Cheers,
Lehel
Thanks but i don’t understand.
Where to add this code? Please help…
its not a solution very cheap code
i want only one link open and ask android default web browser and many other links open to inside the webview
is it possible to explain this using code…….advance thanks
Hi Tabrez,
I think this code will work for you but you need to understand what it does to modify it in order to work for you.
The essence of the code is to:
1. return FALSE
– for URL’s that need to be opened in the webview
2. start a new intent and pass the URL to it AND return TRUE
– for URL’s that you want to open in the default Web Browser of your phone
here is how I would modify it for you
I hope this helps.
Cheers,
Lehel
i cant add
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// open in Browser if your.customurl.com
if (url.contains(“your.customurl.com”) ){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
// all the rest open in Webview
return true;
}
because i already have
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if (url.startsWith(“tel:”) || url.startsWith(“whatsapp:”)) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
i want the booth how ? can help
please help and thank you all