How to implement custom chrome Tab to open url in android JAVA | Kotlin

To implement custom Chrome URL opening in an Android app using Java or kotlin , you can use the Chrome Custom Tabs API. Chrome Custom Tabs provide a way to open URLs in a customized Chrome browser window that retains the look and feel of the user's default browser. Here's all the steps how you can do it:

Step 1 : Add the Chrome Custom Tabs dependency to your app's build.gradle file: 

implementation "androidx.browser:browser:1.5.0"

Step 2 : Create a java / kotlin class with the name of ChromeCustomTabUtil

and write bellow given Code :

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;

public class ChromeCustomTabUtil {

    public static void openCustomTab(Context context, String url) {
        // Set custom tab colors
        int toolbarColor = ContextCompat.getColor(context, R.color.custom_tab_toolbar_color);
        int secondaryColor = ContextCompat.getColor(context, R.color.custom_tab_secondary_color);

        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setToolbarColor(toolbarColor);
        builder.setSecondaryToolbarColor(secondaryColor);

        CustomTabsIntent customTabsIntent = builder.build();
       
        // Launch the custom tab
        customTabsIntent.launchUrl(context, Uri.parse(url));
    }
}


Step 3 : Go To your onClick Method where you want to implement 

// Open a URL using Chrome Custom Tabs String url = "https://www.pankajkcodes.com"; ChromeCustomTabUtil.openCustomTab(this, url);

Summary : After reading this article you can implement custom chrome Tab to  open url in android JAVA | Kotlin 







Post a Comment

Previous Post Next Post