Skip to content

Convenient helper class to launch web page through Google Chrome Custom Tab.

Notifications You must be signed in to change notification settings

YuanLiou/chrome-custom-tab-helper

Repository files navigation

ChromeCustomTabHelper

Convenient helper class to launch web page through Google Chrome Custom Tab.

Download

How to install

  1. Add jcenter() to your the root build.gradle allprojects scope.
allprojects {
    repositories {
        google()
        jcenter()    // <== add this one
    }
}
  1. Add the dependency

Stable version (Android X)

minSdkVersion is 21

implementation "liou.rayyuan.chromecustomtabhelper:chrome-custom-tab-helper:[latest-version]"

How to use

  1. Create a new object for ChromeCustomTabHelper
    private val chromeCustomTabHelper = ChromeCustomTabsHelper() 
  1. Warm up the custom tab service in the onResume() state of Activity or Fragment
    And don't forget to cool down in the onStop() state. You can pick up your prefer browser in the second parameter.
    Current support browsers: Chrome, Firefox and Samsung internet.
    override fun onResume() {
        super.onResume()
        chromeCustomTabHelper.bindCustomTabsServices(this, Browsers.CHROME, url)
    }

    override fun onStop() {
        super.onStop()
        chromeCustomTabHelper.unbindCustomTabsServices(this)
    }
  1. When it need to open web tab.
    val builder = CustomTabsIntent.Builder()
    // here custom your tab styles and behavior
    val chromeCustomTabIntent = builder.build()
    ChromeCustomTabsHelper.openCustomTab(this, Browsers.CHROME, chromeCustomTabIntent, uri) { activity, uri ->
        // if chrome is not install, fallback to standard webview 
        // or just send an intent to someone can handle
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = uri
        startActivity(intent)
    }

That's it!