How to force Custom Tabs to launch as Desktop site everytime? - android

I'm trying to implement custom tabs into my app
https://developer.chrome.com/docs/android/custom-tabs/integration-guide/
I've successfully launched the website using
#OnClick(R.id.btn_launch)
void onLaunchBtnClick(){
String url = "https://paul.kinlan.me/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
}
However it's launching as a mobile site everytime.
How can I force it to launch as a desktop site without having the user clicking checkbox?

Related

How to load chrome custom tab when multiple browsers installed in the device and user has set different browser as default instead of chrome browser?

I am developing one android application in that i need to load all the web URL into chrome custom tab but i am facing one issue when user install multiple browser into their device and they set default browser as different instead of chrome browser in this case chrome tab is not getting open.
I am following this document and sample.
https://developer.chrome.com/multidevice/android/customtabs
https://github.com/GoogleChrome/custom-tabs-client
You can add the Google Chrome package to the customTabsIntent:
CustomTabsIntent tabsIntent = new CustomTabsIntent.Builder().build();
tabsIntent.intent.setPackage("com.android.chrome");
tabsIntent.launchUrl(context, Uri.parse(YOUR_URL));
Try this
public static void CustomTab(Activity activity,
Uri uri) {
// It returns the chrome package name
String packageName = CustomTabsHelper.getPackageNameToUse(activity, mUrl);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
mCustomTabsIntent = builder
.setShowTitle(true)
.build();
builder.setToolbarColor(ContextCompat.getColor(activity, R.color.colorPrimary));
if ( packageName != null ) {
mCustomTabsIntent.intent.setPackage(packageName);
}
mCustomTabsIntent.launchUrl(activity, uri);
}
If user has installed chrome it will open directly

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 :)

Custom Tabs keeps opening web browser

I am trying to load a web url by using Custom Tabs.
I am using following version of custom tabs
implementation 'com.android.support:customtabs:27.1.1'
The issue is that it keeps opening chrome web browser instead of custom tabs type layout descibed in the below picture.
This is my code.
public void createCustomTab(String url){
// CustomTabsIntent.Builder used to configure CustomTabsIntent.
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// CustomTabsIntent used to launch the URL
CustomTabsIntent customTabsIntent = builder.build();
// Open the Custom Tab
customTabsIntent.launchUrl(eContext, Uri.parse(url));
}
I am trying this on a Lollipop device, and my current target SDK is 27

Chrome custom tab crashes if chrome browser is disabled by the user in his device

I am trying to open pdf file in chrome custom tab, it works fine by using this code
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
But the problem is, if the user have disabled chrome from app settings and there is no other browser it crashes. So how can i open it in the chrome custom tab if it is disabled.
Thanks in advance.
this is the exception that is showing up in the logcat.
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://docs.google.com/... (has extras) }
Try this:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
try {
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}
catch (ActivityNotFoundException e)
{
// display message to user
}
Handle your exception like below
try{
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}catch(ActivityNotFoundException e){
// Handle your exception
}
EDIT
If user has disabled it then you can ask user to enable it from settings.
Code for open phone settings
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
REASON behind this, can be easily searched from this SO post.
Written by CommonsWare
Chrome does not appear to export that activity, so it cannot be started by third-party apps.
Even if they did, that behavior could easily vary from version to version of Chrome for Android. Google does not document or support access to such activities from any of their commercial apps, let alone Chrome.

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