Open In App

Android Custom Chrome Tabs using Jetpack Compose

Last Updated : 28 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Many applications display web pages within their android applications to navigate to different websites within the applications. There are different methods with the help of which we can open web pages such as WebView, redirecting to chrome browser, and a custom chrome tabs. Custom chrome tab is the lightest version to load the web pages within the android applications. In this article, we will take a look at How to implement Custom Chrome Tabs in Android applications using Jetpack Compose

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding a new color in the Color.kt file

Navigate to app>java>your app’s package name>ui.theme>Color.kt file and add below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.example.newcanaryproject.ui.theme
 
import androidx.compose.ui.graphics.Color
 
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
 
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 3: Adding dependency in build.gradle file. 

Navigate to Gradle Scripts> build.gradle file and add the below dependency in the dependencies section. 

implementation 'androidx.browser:browser:1.2.0'

After adding this dependency simply sync your project. 

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 inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
 
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Password
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import androidx.core.content.ContextCompat
import com.example.newcanaryproject.ui.theme.*
 
class MainActivity : ComponentActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying background color for our application
                Surface(
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
 
                    // on below line we are specifying theme as scaffold.
                    Scaffold(
 
                        // in scaffold we are specifying top bar.
                        topBar = {
 
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
 
                                // along with that we are specifying title
                                // for our top bar.
                                title = {
 
                                    // in the top bar we are specifying tile as a text
                                    Text(
 
                                        // on below line we are specifying
                                        // text to display in top app bar.
                                        text = "Custom Chrome Tabs",
 
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
 
                                        // on below line we are specifying
                                        // text alignment.
                                        textAlign = TextAlign.Center,
 
                                        // on below line we are specifying
                                        // color for our text.
                                        color = Color.White
                                    )
                                }
                            )
                        }
                    ) {
                        // on below line we are calling session management
                        // method and passing shared preferences to it.
                        customChromeTab()
                    }
                }
            }
        }
    }
}
 
// on below line we are creating a
// function as custom Chrome Tabs.
@Composable
fun customChromeTab() {
    // on below line we are creating a
   // variable for getting context.
    val ctx = LocalContext.current
 
    // on below line we are creating a column
    Column(
        // inside this column we are specifying modifier
        // to column as max size, max height and ,max width.
        modifier = Modifier
            .fillMaxSize()
            .fillMaxWidth()
            .fillMaxHeight(),
 
        // on below line we are adding vertical
        // arrangement and horizontal alignment.
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
 
        // on the below line we are creating a button.
        Button(
 
            // on below line we are adding modifier to our button
            // for adding max width and adding padding to button
            // from all sides.
            modifier = Modifier
                .fillMaxWidth()
                .padding(18.dp),
 
            // on below line we are adding color for our button.
            colors = ButtonDefaults.buttonColors(backgroundColor = greenColor),
 
            // on below line we are adding on click for our button.
            onClick = {
 
                // on below line we are creating an open tab method
                // to open our custom chrome tabs.
                openTab(ctx)
 
            })
        // on the below line we are creating
        // a text for our button
        // and adding padding to our text.
        {
            Text(text = "Open custom chrome tab", modifier = Modifier.padding(8.dp))
 
        }
 
    }
}
 
// on below line we are creating a function to open custom chrome tabs.
fun openTab(context: Context) {
    // on below line we are creating a variable for
    // package name and specifying package name as
    // package of chrome application.
    val package_name = "com.android.chrome"
 
    // on below line we are creating a variable for
    // our URL which we have to open in chrome tabs
    val URL = "https://www.geeksforgeeks.org"
 
    // on below line we are creating a variable
    // for the activity and initializing it.
    val activity = (context as? Activity)
 
    // on below line we are creating a variable for
    // our builder and initializing it with
    // custom tabs intent
    val builder = CustomTabsIntent.Builder()
 
    // on below line we are setting show title
    // to true to display the title for
    // our chrome tabs.
    builder.setShowTitle(true)
 
    // on below line we are enabling instant
    // app to open if it is available.
    builder.setInstantAppsEnabled(true)
 
    // on below line we are setting tool bar color for our custom chrome tabs.
    builder.setToolbarColor(ContextCompat.getColor(context, R.color.purple_200))
 
    // on below line we are creating a
    // variable to build our builder.
    val customBuilder = builder.build()
 
    // on below line we are checking if the package name is null or not.
    if (package_name != null) {
        // on below line if package name is not null
        // we are setting package name for our intent.
        customBuilder.intent.setPackage(package_name)
 
        // on below line we are calling launch url method
        // and passing url to it on below line.
        customBuilder.launchUrl(context, Uri.parse(URL))
    } else {
        // this method will be called if the
        // chrome is not present in user device.
        // in this case we are simply passing URL
        // within intent to open it.
        val i = Intent(Intent.ACTION_VIEW, Uri.parse(URL))
 
        // on below line we are calling start
        // activity to start the activity.
        activity?.startActivity(i)
    }
 
}


Now run your application to see the output of it. 

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads