Created
February 19, 2016 15:07
-
-
Save thejakeofink/2278939018974e7c2ab3 to your computer and use it in GitHub Desktop.
Chrome Custom Tabs example in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thejakeofink.chromecustomtabs | |
import android.content.ComponentName | |
import android.graphics.Color | |
import android.net.Uri | |
import android.os.Bundle | |
import android.support.customtabs.CustomTabsClient | |
import android.support.customtabs.CustomTabsIntent | |
import android.support.customtabs.CustomTabsServiceConnection | |
import android.support.customtabs.CustomTabsSession | |
import android.support.v7.app.AppCompatActivity | |
import android.widget.Button | |
/** | |
* This example is using the Chrome Custom Tab Support Library | |
* make sure to include: | |
* compile 'com.android.support:customtabs:<version_number e.g. 23.1.1>' | |
* in your build dependencies | |
*/ | |
class ChromeCustomTabsActivity : AppCompatActivity() { | |
val CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; | |
private var mCustomTabsServiceConnection: CustomTabsServiceConnection? = null | |
private var mClient: CustomTabsClient? = null | |
private var mCustomTabsSession: CustomTabsSession? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mCustomTabsServiceConnection = object : CustomTabsServiceConnection() { | |
override fun onCustomTabsServiceConnected(componentName: ComponentName, customTabsClient: CustomTabsClient) { | |
//Pre-warming | |
mClient = customTabsClient | |
mClient?.warmup(0L) | |
mCustomTabsSession = mClient?.newSession(null) | |
} | |
override fun onServiceDisconnected(name: ComponentName) { | |
mClient = null | |
} | |
} | |
CustomTabsClient.bindCustomTabsService(this, CUSTOM_TAB_PACKAGE_NAME, mCustomTabsServiceConnection); | |
} | |
override fun onResume() { | |
super.onResume() | |
val google = findViewById(R.id.google_btn) as Button | |
val vivint = findViewById(R.id.vivint_btn) as Button | |
val geek = findViewById(R.id.geek_btn) as Button | |
val ksl = findViewById(R.id.ksl_btn) as Button | |
val parallax = findViewById(R.id.parallax_btn) as Button | |
google.setOnClickListener { | |
loadCustomTabForSite("http://google.com") | |
} | |
vivint.setOnClickListener { | |
loadCustomTabForSite("http://vivintsky.com", resources.getColor(R.color.vivint_orange)); | |
} | |
geek.setOnClickListener { | |
loadCustomTabForSite("http://www.dividendgeek.com", resources.getColor(R.color.colorPrimary)); | |
} | |
ksl.setOnClickListener { | |
loadCustomTabForSite("http://www.ksl.com", resources.getColor(R.color.colorAccent)) | |
} | |
parallax.setOnClickListener { | |
loadCustomTabForSite("http://matthew.wagerfield.com/parallax/", Color.RED); | |
} | |
} | |
fun loadCustomTabForSite(url: String, color: Int = Color.BLUE) { | |
val customTabsIntent = CustomTabsIntent.Builder(mCustomTabsSession) | |
.setToolbarColor(color) | |
.setShowTitle(true) | |
.build() | |
customTabsIntent.launchUrl(this, Uri.parse(url)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment