カスタムタブの利点の一つは、アプリにシームレスに統合できることです。カスタムタブ ガイドのこのパートでは、アプリに合わせてカスタムタブの外観と動作を変更する方法について説明します。
ツールバーの色を設定する
まず、カスタムタブのアドレスバーをカスタマイズして、アプリのテーマと一致させます。以下のスニペットでは、setDefaultColorSchemeParams() を呼び出してデフォルトのツールバーの色を変更しています。アプリがダーク カラーパターンもサポートしている場合は、.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, …) で設定します。
// get the current toolbar background color (this might work differently in your app)
@ColorInt val colorPrimaryLight = ContextCompat.getColor(
this@MainActivity,
R.color.md_theme_light_primary
)
@ColorInt val colorPrimaryDark = ContextCompat.getColor(
this@MainActivity,
R.color.md_theme_dark_primary
)
val intent = CustomTabsIntent.Builder()
// set the default color scheme
.setDefaultColorSchemeParams(
CustomTabColorSchemeParams.Builder()
.setToolbarColor(colorPrimaryLight)
.build()
)
// set the alternative dark color scheme
.setColorSchemeParams(
CustomTabsIntent.COLOR_SCHEME_DARK,
CustomTabColorSchemeParams.Builder()
.setToolbarColor(colorPrimaryDark)
.build()
)
.build()
ツールバーにカスタムの背景色と前景色が設定されました。
カスタムの開始時と終了時のアニメーションを構成する
次に、setStartAnimation と setExitAnimation を使用してカスタムの開始アニメーションと終了アニメーションを定義することで、アプリでカスタムタブを起動(および終了)する際のエクスペリエンスをよりシームレスにできます。
val intent = CustomTabsIntent.Builder()
// …
.setStartAnimations(
this@MainActivity,
R.anim.slide_in_right,
R.anim.slide_out_left
)
.setExitAnimations(
this@MainActivity,
android.R.anim.slide_in_left,
android.R.anim.slide_out_right
)
.build()
その他のカスタマイズ: タイトル、AppBar の自動非表示、カスタムの閉じるアイコン
カスタムタブの UI をニーズに合わせて調整するためにできることは他にもいくつかあります。
- スクロール時に URL バーを非表示にして、
setUrlBarHidingEnabled()(true)を使用してウェブ コンテンツを探索するためのスペースをユーザーに提供します。 setShowTitle()(true)を使用して、URL ではなくドキュメントのタイトルを表示します。- アプリのユーザーフローに合わせて閉じるボタンをカスタマイズします。たとえば、デフォルトの
Xアイコンの代わりに、戻る矢印を表示します。setCloseButtonIcon()(myCustomCloseIcon)
これらはすべてオプションですが、アプリでのカスタムタブのエクスペリエンスを向上させることができます。
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.toBitmap
// …
val myCustomCloseIcon = AppCompatResources
.getDrawable(this, R.drawable.ic_baseline_arrow_back_24)
?.toBitmap()
val intent = CustomTabsIntent.Builder()
// …
.setUrlBarHidingEnabled(true)
.setShowTitle(true)
.apply { myCustomCloseIcon?.let { setCloseButtonIcon(it) } }
.build()
カスタム参照元の設定
カスタムタブを起動するときに、アプリをリファラーとして設定できます。これにより、ウェブサイトはトラフィックの発生元を把握できます。
val customTabsIntent = CustomTabsIntent.Builder().build()
customTabsIntent.intent.putExtra(
Intent.EXTRA_REFERRER,
Uri.parse("android-app://${context.packageName}")
)
次のステップ: カスタムタブにカスタム アクションを追加する方法