UI のカスタマイズ

カスタムタブの利点の一つは、アプリにシームレスに統合できることです。カスタムタブガイドのこのパートでは、アプリに合わせてカスタムタブの外観と動作を変更する方法について説明します。

ツールバーの色を設定する

カスタムライト カラーパターンのカスタムタブ
ライトモード
カスタム ダーク カラーパターンのカスタムタブ
ダークモード

まず、アプリのテーマに合わせてカスタムタブのアドレスバーをカスタマイズします。以下のスニペットでは、setDefaultColorSchemeParams() を呼び出してツールバーのデフォルトの色を変更しています。アプリがダーク カラーパターンもサポートしている場合は、.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, …) で設定します。

// get the current toolbar background color (this might work differently in your app)
@ColorInt int colorPrimaryLight = ContextCompat.getColor(MainActivity.this, R.color.md_theme_light_primary);
@ColorInt int colorPrimaryDark = ContextCompat.getColor(MainActivity.this, R.color.md_theme_dark_primary);

CustomTabsIntent intent = new CustomTabsIntent.Builder()
        // set the default color scheme
        .setDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder()
                .setToolbarColor(colorPrimaryLight)
                .build())
        // set the alternative dark color scheme
        .setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, new CustomTabColorSchemeParams.Builder()
                .setToolbarColor(colorPrimaryDark)
                .build())
        .build();

ツールバーにカスタムの背景色と前景色が追加されました。

カスタムの開始と終了のアニメーションを設定する

次に、setStartAnimationsetExitAnimation を使用してカスタムの開始と終了のアニメーションを定義することで、アプリでのカスタムタブの起動(および終了)をよりシームレスにすることができます。

CustomTabsIntent intent = new CustomTabsIntent.Builder()
…
.setStartAnimations(MainActivity.this, R.anim.slide_in_right, R.anim.slide_out_left)
.setExitAnimations(MainActivity.this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.build();

その他のカスタマイズ: タイトル、アプリバーの自動非表示、カスタムの閉じるアイコン

必要に応じて、カスタムタブの UI を調整する方法がいくつかあります。

  • スクロール時に URL バーを非表示にして、ユーザーが setUrlBarHidingEnabled()(true) を使用してウェブ コンテンツを探索するためのスペースを増やします。
  • setShowTitle()(true) により、URL の代わりにドキュメントのタイトルを表示します。
  • アプリのユーザーフローに合わせて閉じるボタンをカスタマイズします(たとえば、デフォルトの X アイコンの代わりに戻る矢印を表示します): setCloseButtonIcon()(myCustomCloseIcon)

これらはすべて任意ですが、実装するとアプリのカスタムタブのエクスペリエンスが向上します。

Bitmap myCustomCloseIcon = getDrawable(R.drawable.ic_baseline_arrow_back_24));
CustomTabsIntent intent = new CustomTabsIntent.Builder()
  …
  .setUrlBarHidingEnabled(true)
  .setShowTitle(true)
  .setCloseButtonIcon(toBitmap(myCustomCloseIcon))
  .build();

カスタム リファラーの設定

カスタムタブを起動するときに、参照 URL としてアプリを設定できます。これにより、トラフィックの発生元をウェブサイトに知らせることができます。

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build()
customTabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
       Uri.parse("android-app://" + context.getPackageName()));

次のステップ: カスタムタブにカスタム操作を追加する方法