Open In App

Chrome Custom Tabs in Android with Kotlin

Last Updated : 30 Aug, 2021
Like Article

As developers, we have the option of displaying web content to a user in their browser or via WebViews, but both have their own limitations: starting the browser is a large, non-customizable context transition for users, whereas WebViews don’t support all aspects of the web platform. To address this issue, Google launched chrome custom tabs. It is a browser feature that provides apps with more control over their web experiences and enables more seamless transitions between native and web content without the usage of a WebView. They allow developers to alter the appearance and feel of the browser. It offers numerous advantages and customizations:

  • Ability to change toolbar color
  • Add enter and exit animations
  • Enable content sharing
  • Add custom actions to the browser toolbar and overflow menu
  • Optimizes the performance

What we are going to build in this article? 

In this article, we will be using Chrome customs tabs to display the web content to users with several customizations. To give you an idea of what we’ll be doing in this article, here’s a sample video.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add the library dependency

Navigate to the Gradle Scripts > build.gradle(Module: app), add the library in the dependencies section, and sync the project.

dependencies {
      implementation 'androidx.browser:browser:1.3.0'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
      <!--Adding a button-->
        android:text="Open Custom Chrome Tabs"

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added to understand the code in more detail.


import android.content.Context
import android.os.Bundle
import androidx.browser.customtabs.*
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
    private var GFG_URI = ""
    private var package_name = "";
    override fun onCreate(savedInstanceState: Bundle?) {
        button.setOnClickListener {
            val builder = CustomTabsIntent.Builder()
            // to set the toolbar color use CustomTabColorSchemeParams
            // since CustomTabsIntent.Builder().setToolBarColor() is deprecated
            val params = CustomTabColorSchemeParams.Builder()
            params.setToolbarColor(ContextCompat.getColor(this@MainActivity, R.color.colorPrimary))
            // shows the title of web-page in toolbar
            // setShareState(CustomTabsIntent.SHARE_STATE_ON) will add a menu to share the web-page
            // To modify the close button, use
            // builder.setCloseButtonIcon(bitmap)
            // to set weather instant apps is enabled for the custom tab or not, use
            //  To use animations use -
            //  builder.setStartAnimations(this, android.R.anim.start_in_anim, android.R.anim.start_out_anim)
            //  builder.setExitAnimations(this, android.R.anim.exit_in_anim, android.R.anim.exit_out_anim)
            val customBuilder =
            if (this.isPackageInstalled(package_name)) {
                // if chrome is available use chrome custom tabs
                customBuilder.launchUrl(this, Uri.parse(GFG_URI))
            } else {
                // if not available use WebView to launch the url
fun Context.isPackageInstalled(packageName: String): Boolean {
    // check if chrome is installed or not
    return try {
        packageManager.getPackageInfo(packageName, 0)
    } catch (e: PackageManager.NameNotFoundException) {


Project Link: Click Here

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads