I convert WordPress website to android app, in my website lots of external links i need to open external links browser like telegram, in telegram all external links open in external browser in the app
3 Answers
As @peshkira has given a good solution, but I would suggest to use CustomTab.
With Custom Tabs you can get instance of browser without creating a web view. As it provide navigation awareness, the browser delivers a callback to the application upon an external navigation. You can modify and update following -
Custom menu
Color of the address bar
Custom action button
Custom enter and exit animations
Setup for CustomTab -
dependencies {
...
implementation "androidx.browser:browser:1.2.0"
}
In your code, Open a CustomTab -
String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
You can save up to 700 ms when opening a link with the Custom Tabs by connecting to the service and pre-loading the browser.
Most browser do support CustomTab like Chrome, Samsung Mobile Broswer, Microsoft Edge etc. Please follow Custom Tabs Best Practices. Here is a gitHub example.
Edit
As you mentioned in your comment "every post have 1 external link", then you following options -
- Create a
WebView
to open link - Use intent to open device external browser
- Use
CustomTab
to open instance of browser
I think behavior of all three is very well explained via this gif.
Now, to show all the posts in your app, I believe you are using a RecycleView
, so you can decide from three choices, you can listen for click on one of the post by using a click listener on that post, something like follows -
postItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClickOnLink(postUrl);
}
});
Now in the body of your method handleClickOnLink(String postUrl)
you can use your code to decide how you want open the link.
As I have mentioned above, Using CustomTab
will be one of the choices, which I think will be a good option in this case, so you can have an implementation of method as follows -
public void handleClickOnLink(String postUrl){
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
//setting toolbar color
builder.setToolbarColor(getContext().getColor(R.color.colorPrimaryDark));
//can be more modification as builer.setTitle() and more
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getContext(), Uri.parse(postUrl));
}
If there is a device with a browser which doesn't support CustomTab
then device default browser will be open.
Result will be something as follows -

- 897
- 1
- 4
- 18
-
bro in webview how to work? already i have a wordpress site i covert site to app, in that site every post have 1 external links so how to do this – jilin james Aug 15 '20 at 20:51
-
well I'm not sure how you have converted your wordpress site into your app, if you're already using a WebView to show content of your wordpress site then using CustomTab is not in picture at all, you should go with `shouldOverrideUrlLoading` suggested by @peshkira.. if you are using some `WebCrawler` or some other way to get information from your site to show in a list then please check my edit. – Nikhil Sharma Aug 16 '20 at 03:10
-
more over, I don't use telegram but I did check the external link behavior on telegram on one of my friend's phone and I found telegram do have implementation of `CustomTab` in their code for external links. – Nikhil Sharma Aug 16 '20 at 04:25
-
Dear Sharma, I need to do other side, i need to prevent going to the default browser, i am loading a asp web site to my web view, when press log in button the web view is requesting to go to the default browser. i need to stop it and going to the main form thought android web view. how do i fixed this please? – lulitha Mar 26 '23 at 08:40
You can add a WebViewClient
to your webview and override the shouldOverrideUrlLoading
method. This will give you a callback when a link is clicked and you can inspect the url and decide if you want to open in the webview or open it in the external browser.
For example something like:
private class MyCustomWebClient : WebViewClient() {
@TargetApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
) = if (interceptUrlLoading(request?.url?.toString())) {
true
} else {
super.shouldOverrideUrlLoading(view, request)
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean =
if (interceptUrlLoading(url)) {
true
} else {
super.shouldOverrideUrlLoading(view, url)
}
private fun interceptUrlLoading(url: String?): Boolean {
return if (url.equals("telegram url ...")) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
true
} else {
false
}
}
}

- 6,069
- 1
- 33
- 46
Problem SOlved guys thanks for your replays
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;
}
}
});

- 11
- 1
- 5