The above quick integration example will open your Uri on a Chrome Custom Tab without warming up, pre-fetching or UI customizations. openCustomTab ( this, customTabsIntent, uri, new CustomTabActivityHelper. If the user doesn't have a browser that supports Chrome Custom Tabs, it will open the default browser: CustomTabsIntent customTabsIntent = new CustomTabsIntent. Use the following method to open a Chrome Custom Tab if possible. build () Enable pre-starting and pre-fetchingĬopy the following files from GoogleChrome sample git repo to your project and adjust the package names accordingly: setActionButton ( bitmap, "Share Link", pendingIntent, true ) CustomTabsIntent customTabsIntent = builder. Builder () // Map the bitmap, text, and pending intent to this icon // Set tint to be true so it matches the toolbar color builder. FLAG_UPDATE_CURRENT ) įinally, we need to pass the bitmap, text, and pending intent created: CustomTabsIntent. getActivity ( this, requestCode, intent, PendingIntent. This pending intent needs to be passed to the Chrome Tabs intent builder: int requestCode = 100 PendingIntent pendingIntent = PendingIntent. Next, you need to create a pending intent (see this YouTube clip for more info), which is used to wake up your app when the user clicks on the icon. Next, create the intent: Intent intent = new Intent ( Intent. We will need to create a bitmap for use later: Bitmap bitmap = BitmapFactory. If you want to add a specific icon (such as the share icon) to the toolbar, you need to first should add the icon using New -> Image Asset (currently, Chrome Tabs do not support vector drawables so you should be using PNG files as your icons): addDefaultShareMenuItem () Adding custom icons Builder () // add share action to menu list builder. However, you can add a default one to the menu item list: CustomTabsIntent. Adding a share actionīy default, a Chrome tab does not include share action in the toolbar. For this reason, see this guide for how to include the design support library to leverage a new ContextCompat API. Normally, context.getResources().getColor()) can be used, but in Android API 23 this method has been deprecated. If you wish to set the toolbar color, you can use the setToolbarColor() method in the builder class: CustomTabsIntent. If you wish to add custom icons or actions to the menu, you will need to create pending intents to do so. Configuring other optionsĬonfiguring additional options requires using the builder class. SESSION and .TOOLBAR_COLOR) that gets ignored if the default browser cannot process this information. The CustomTabsIntent simply launches an implicit intent ( ) and passes an extra data in the intent (i.e. If you do not have Chrome installed, the intent will launch the default browser installed on the device. build () // and launch the desired Url with CustomTabsIntent.launchUrl() customTabsIntent. Builder () // set toolbar color and/or setting custom actions before invoking build() // Once ready, call () to create a CustomTabsIntent CustomTabsIntent customTabsIntent = builder. The most basic example to launch a Chrome tab is through a custom intent as shown below: // Use a CustomTabsIntent.Builder to configure CustomTabsIntent. If you are supporting previous API, you can add to your manifest to force its use and check the API version at runtime and just use Chrome Custom tabs if its >= 16 Usage The library only works on API 16 (Jelly Bean) an above. If you are using an emulator, you must setup Google Play Services and install the Chrome app though the Play store.Īdd the Android Support Library for Chrome Custom Tabs as a dependency to your gradle build file: implementation ':customtabs:25.2.0' You will need to have the Chrome app installed on your phone. See this README for more ways to use Chrome Custom Tabs. Add custom actions to the Chrome toolbar and overflow menuĬhrome custom tabs also allow the developer to pre-start Chrome and pre-fetch content for faster loading.Chrome custom tabs give apps more control over their web experience, and make transitions between native and web content more seamless without having to resort to a WebView.Ĭhrome custom tabs allow an app to customize how Chrome looks and feels.
0 Comments
Leave a Reply. |