Using the Custom Tab low level API

The recommended way to integrate your application with Custom Tabs is using the AndroidX Browser Library, but you can also start a Custom Tab without the support library. This guide gives an overview on how to achieve this.

The complete implementation of the Support Library is available onGitHub and can be used as astarting point. It also contains theAIDL files required to connect to the service, as the onescontained in the Chromium repository are not directly usable with Android Studio.

Basics for Launching Custom Tabs using the Low Level API

// Using a VIEW intent for compatibility with any other browsers on device.// Caller should not be setting FLAG_ACTIVITY_NEW_TASK or// FLAG_ACTIVITY_NEW_DOCUMENT.Stringurl=¨https://paul.kinlan.me/¨;Intentintent=newIntent(Intent.ACTION_VIEW,Uri.parse(url));//  Must have. Extra used to match the session. Its value is an IBinder passed//  whilst creating a news session. See newSession() below. Even if the service is not//  used and there is no valid session id to be provided, this extra has to be present//  with a null value to launch a custom tab.privatestaticfinalStringEXTRA_CUSTOM_TABS_SESSION="android.support.customtabs.extra.SESSION";Bundleextras=newBundle;extras.putBinder(EXTRA_CUSTOM_TABS_SESSION,sessionICustomTabsCallback.asBinder()/* Set to null for no session */);intent.putExtras(extras);

Adding UI Customizations

UI Customizations are included by adding Extras to the ACTION_VIEW Intent. A full list of theextras keys used to customize the UI can be found on theCustomTabsIntent docs. An example onhow to add a custom toolbar color follows:

// Extra that changes the background color for the address bar. colorInt is an int// that specifies a Color.privatestaticfinalStringEXTRA_CUSTOM_TABS_TOOLBAR_COLOR="android.support.customtabs.extra.TOOLBAR_COLOR";intent.putExtra(EXTRA_CUSTOM_TABS_TOOLBAR_COLOR,colorInt);

Connecting to the Custom Tabs service

The Custom Tabs service can be used in the same way other Android Services are. The interface iscreated with AIDL and automatically creates a proxy service class for you.

Use the methods on the proxy service to warm-up, create sessions and pre-fetch:

// Package name for the Chrome channel the client wants to connect to. This// depends on the channel name.// Stable = com.android.chrome// Beta = com.chrome.beta// Dev = com.chrome.devpublicstaticfinalStringCUSTOM_TAB_PACKAGE_NAME="com.chrome.dev";// Change when in stable// Action to add to the service intent. This action can be used as a way// generically pick apps that handle custom tabs for both activity and service// side implementations.publicstaticfinalStringACTION_CUSTOM_TABS_CONNECTION="android.support.customtabs.action.CustomTabsService";IntentserviceIntent=newIntent(ACTION_CUSTOM_TABS_CONNECTION);serviceIntent.setPackage(CUSTOM_TAB_PACKAGE_NAME);context.bindService(serviceIntent,mServiceConnection,Context.BIND_AUTO_CREATE|Context.BIND_WAIVE_PRIORITY);

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2020-02-04 UTC.