Chrome Custom Tabs code for Android Studio

Welcome. Here in this tutorial you will learn how to add Chrome custom tabs in Android Studio.

Chrome Custom Tabs Example for Android Studio

In Android Studio, when you want to open any URL in your app, there were generally two options available. They are

  1. Opening in a new tab
  2. Using Webviews

But both of these two methods had their own problems. To solve this, chrome introduced an alternate solution with the concept of Chrome Custom Tabs. This is more customizable and make transitions among local and web content progressively consistent without falling back on a WebView.

Adding a Chrome Custom tab in your App

To add a chrome custom tab, first of all you will have to add its dependency to your android build.gradle file. Just add the latest version of the below given dependency.

To launch a custom tab, you can use the  following code:-

You can also set the toolbar colour of your new chrome tab by using this code:-

To add the share option in the new chrome tab menu bar, add this line:-

Also, if you want to show the title of the chrome custom tab, add this line:-

An image is shown below for demo purpose:-

The Full Java Code along with the XML layout for the above example is shown below:-

  • First Create a new project in Android Studio
  • File ⇒ New Android ⇒ Application Project
  • Then Open src -> package -> MainActivity.java and then add following code :

JAVA (MainActivity.java)

  •  Now Open res -> layout -> activity_main.xml and then add following code :

XML (activity_main.xml)

Output:

Finally, run this project.

This is how Chrome Custom Tabs code 

Chrome Custom Tabs code Chrome Custom Tabs code

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with other custom tab options on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your chrome custom tab look the way you want it to be.

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.chrome.com/multidevice/android/customtabs

With this we came to an end of our tutorial. Comment down below if you are facing any problems.

 

Also Read:-

Add a Comment

Your email address will not be published. Required fields are marked *