Home » Android » android – Custom Tabs Support Library

android – Custom Tabs Support Library

Posted by: admin April 23, 2020 Leave a comment

Questions:

Recently was added this support library, but I couldn’t find any example.

What the purpose of this library?

Could you post any example using this library?

How to&Answers:

CustomTabs is used to open links in a browser that supports CustomTabs. Most likely opening is done on Chrome, hence CustomTabs is part of chromium platform.

Purpose is to avoid implementing WebViews in your application and yet giving you option for styling actual chrome tabs, like toolbar color, title, various exit/enter transition, adding action buttons and menues. CustomTabs will allow your application bind to the chrome service and make chrome work as part of your application. Styling which will give you feel the opened web resource is part of your application.

Beside the styling, CustomTabs will give you full chrome web capabilities that probably couldn’t be achieved with standard WebView.

Here are demos, which are straight forward.

Edit:

A snippet from my application which is “simplified” version of the Google’s demo, lacking fallback mechanism, for now.

Usage of the helper is the following:

  1. Initialize it when your activity is alive

      @Override
      protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_preview);
         mCustomTabHelper = new SimpleCustomChromeTabsHelper(this);
}


2. When the instance is alive and we have an url ready to be opened we can call:

mCustomTabHelper.prepareUrl(mProduct.getRedirectUrl());

Which will bind to the Chrome service, if not previously bind, or will just notify Chrome service that we might be opening that link in the future.
CustomTabSession can be used to open or prepare multiple url.

  1. Open the url

    mCustomTabHelper.openUrl(mProduct.getRedirectUrl());

The overloaded method of openUrl is using sort of ui options builder that is replica of the CustomTabIntent.Builder, but I have dropped the CustomTabsSession argument so the helper later will build CustomTabIntent internally.

I'm running Chrome Dev version along stable one. If I choose the stable one, I'm not able to use CustomTabsat all. As Google advices, CustomTabs will only work on Chrome 45 and beta versions of Chrome.

Demo from my application: https://youtu.be/fnIZwuJXjHI

Edit: Post

Answer:

Try this:

gradle dependency:

dependencies {
    ...
    compile 'com.android.support:customtabs:25.1.0'
}

Code :

Uri uri = Uri.parse("https://github.com/mzelzoghbi");

// create an intent builder
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();

// Begin customizing
// set toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));

// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();

// launch the url
customTabsIntent.launchUrl(activity, uri);

Answer:

There is demo project on github, mentioned by @NikolaDespotoski, which can be partially reusable.

Solution is based on this article.

  1. Add project shared to your project. Shared is a name of project (I don't know why Google didn't add it into customtabs library). link to shared project

  2. Copy Activity helper from demo project to your project and put correct package. CustomTabActivityHelper

  3. To pre-fetch url use CustomTabActivityHelper#mayLaunchUrl method (if needed) and CustomTabActivityHelper#openCustomTab to open Chrome custom tab.

For instance openning custom tab:

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri,
        new CustomTabActivityHelper.CustomTabFallback() {
            @Override
            public void openUri(Activity activity, Uri uri) {
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

Pre-fetching of url is more complicated. You can see this demo for better understanding.