How to rewrite the code to open link on Chrome custom tab - android

How can I rewrite the code to open link on Chrome custom tab:
imageView5 = (AppCompatImageView) findViewById(R.id.image_view5);
imageView5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view3) {
Intent intent1 =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://mekotube.com/muslim/"));
((HOMEActivity) getContext()).startActivity(intent1);
}
});

The documentation for Custom Tabs is available here.
On the Custom Tabs Github repo, There is a set of demos, showing how to use the API.
Check out the simple demo app, showing how to open it.
I'd also recommend checking out the demo on how to use the Custom Tabs Service to improve speed when opening the page.
Make sure to check out the best practices as well, as they contain valuable hints on how to handle multiple browsers among other stuff.
Here's an extract from the docs, showing how to open a simple tab, without any customisation or connecting to the Custom Tabs service.
1 - Add the Custom Tabs Support Library to your Gradle build file.
dependencies {
...
implementation 'com.android.support:customtabs:28.0.0'
}
2 - Use the Support Library to open a link:
String url = "https://paul.kinlan.me/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

Related

Chrome Custom Tabs customisations like setToolbarColor, start and exit animations are not working

I am new to Android and trying to open link in Chrome Custom Tabs. I have created a funtion to open any links on the app in Chrome Custom Tabs:
public static void chromeCustomTab(Context context, String url)
{
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(context,Uri.parse(url));
builder.setToolbarColor(context.getResources().getColor(R.color.colorPrimaryDark));
builder.setStartAnimations(context, R.anim.slide_in_bottom, R.anim.slide_out_top);
builder.setExitAnimations(context, R.anim.slide_in_top, R.anim.slide_out_bottom);
}
Now the links are opening in Chrome Custom Tabs, but none of the cosutomizations are working. The ToolbarColor is not changing, the animations are not working.
Also, the site title is not visible on the Chrome Custome Tab, only the base url is visible.
Please help me where I am going wrong on this.
add the builder settings BEFORE you launch the url :)

Disable swipe to refresh in chrome CustomTab

I'm using chrome CustomTab to open a url inside my app, using customTabsIntent.launchUrl method and CustomTabsIntent.Builder. Here's how I'm doing it right now:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(someUrl);
is there any way to disable the swipetorefresh behaviour that is set by default, in the CustomTab? by modifying the builder or something? (without making any changes to the website)
No, This is not currently possible, as Custom Tabs will respect the behaviour of the opened web page. If changing the page is possible, these instructions can help

In Kotlin how do I open a link in new Window

I am a tyro in Kotlin but I have a good Knowledge of Android and Core java. I am stuck on one condition while developing an android app via the Kotlin assistance.
I want that when user clicks a link present on the pdf document; the link should be opened on a browser (and if browser is opened then the link should open on the new window not new tab of the same window ).
I have achieved much of my objective but I didn't found out how to open a link in the new Window if the browser is open already?
I have tried the code below(when the link on the pdf is clicked then it redirects to the below function call):
fun web_page_open(urls: String) { // for more than one url
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
startActivity(intents)
}
I have tried my level-best to explain my problem and also searched a lot(on github as well) but all my efforts went in vein.
Any help is warmly welcomed.
EDIT: Let's consider an instance if the user has already opened the default browser (say ABZfox) then when the link inside the pdf(or a doc) is clicked then the new Window of ABZfox opens instead of the same window in which the user was previously working. I'm sure the question makes some sense now!!!
You can try this one, might be helpful, open new tab of web browser like
fun openNewTabWindow(urls: String, context : Context) {
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
val b = Bundle()
b.putBoolean("new_window", true)
intents.putExtras(b)
context.startActivity(intents)
}
You may use chrome custom tabs instead
To use it you need to add below dependency to your gradle
compile 'com.android.support:customtabs:23.1.1'
Now use below code to open the url
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.enableUrlBarHiding();
builder.setShowTitle(true);
builder.setToolbarColor(Color.TRANSPARENT);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));

Handling External Links in android WebView like Gmail App does

I'm a web developer. I'm currently developing android application on Android Studio using WebView which access my website as an android application. One of my webpage contains many external links. My goal is to make the android application can handle external links like Gmail App does (also like facebook and Line do).
Below is the example of gmail app.
An email contains external link
Link clicked, then application open a new activity acts like a browser without leaving Gmail application
Any idea how to make it?
It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.
First add this dependency to your build.gradle(Module:app)
compile 'com.android.support:customtabs:23.4.0'
Second add below function to your code and simply pass string URL to it.
private void redirectUsingCustomTab(String url)
{
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// set desired toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// add start and exit animations if you want(optional)
/*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);*/
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity, uri);
}
Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.
Hope it will help you to start with :)

How to add EXTRA_REFERRER to CustomTabsIntent builder in Chrome custom tab for Android

I am using newly launched Chrome Custom tabs for android instead of using webviews. This is the link to their documentation
Here is the code that shows how to use it.
String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
Question is that I want to add Intent.EXTRA_REFERRER to this. below is the para copied from their blog in their own words..
It's usually very important for websites to track where their traffic
is coming from. Make sure you let them know you are sending them users
by setting the referrer when launching your Custom Tab
intent.putExtra(Intent.EXTRA_REFERRER,
Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));
I failed to figure out any intent created to launch custom tabs.. Where to add this line?? If somebody has came across this, help please.
you can put the extra on the Intent that is inside the CustomTabsIntent created by the builder like the following:
String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
Uri.parse("android-app://" + context.getPackageName()));
customTabsIntent.launchUrl(this, Uri.parse(url));
Explanation: Under the hood, a Custom tab is opened by using regular a Intent with a set of Extras that configure the UI customization. It's possible to see more on how it works at the Low Level API section of the docs. When CustomTabsIntent.Builder#build() is called, it creates a CustomTabsIntent, with a properly configured Intent inside it. This intent is still exposed by the API and that's what we use to add the EXTRA_REFERRER.

Categories

Resources