このガイドでは、カスタムタブにカスタム インタラクティビティを追加する方法について説明します。
デフォルトの共有アクションを有効にする
カスタム共有アクションを指定しない場合は、オーバーフロー メニューでブラウザのデフォルトの共有アクションを有効にして、ユーザーが表示しているコンテンツへのリンクを簡単に共有できるようにすることをおすすめします。
val intentBuilder = CustomTabsIntent.Builder()
intentBuilder.setShareState(CustomTabsIntent.SHARE_STATE_ON)
カスタム アクション ボタンを追加する
重要なアクションについては、カスタムタブのツールバーにカスタム アクション ボタンを統合できます。このボタンには、テキストラベルまたはカスタム アイコンを設定できます。アイコンの高さは 24 dp、幅は 24 ~ 48 dp にする必要があります。

たとえば、カスタムの共有アクションをツールバーに追加できます。これを行うには、ユーザーがカスタムタブで共有アクションをクリックしたときに呼び出される BroadcastReceiver を作成します。
AndroidManifest.xml ファイルに BroadCastReceiver を登録します。
<application …>
<receiver android:name=".ShareBroadcastReceiver" />
</application>
次に、新しいクラス ShareBroadcastReceiver を追加します。onReceive() メソッドで、現在表示されている URL をインテントから抽出し、送信インテントをトリガーします。
class ShareBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val url = intent.dataString
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, url)
sendIntent.type = "text/plain"
val shareIntent = Intent.createChooser(sendIntent, null)
shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(shareIntent)
}
}
次に、ShareBroadcast の PendingIntent を作成し、setActionButton() を介して登録します。アイコンと説明とともにペンディング インテントを渡します。
val shareDescription = getString(R.string.label_action_share)
val shareIcon = BitmapFactory.decodeResource(
resources,
android.R.drawable.ic_menu_share
)
// Create a PendingIntent to your BroadCastReceiver implementation
val actionIntent = Intent(
this.applicationContext,
ShareBroadcastReceiver::class.java
)
val pendingIntent = PendingIntent.getBroadcast(
applicationContext,
0 /* request code */,
actionIntent,
PendingIntent.FLAG_MUTABLE
)
// Set the pendingIntent as the action to be performed when the button is clicked.
val intentBuilder = CustomTabsIntent.Builder()
// …
.setActionButton(shareIcon, shareDescription, pendingIntent)
.build()
カスタム メニュー項目を追加する
カスタムタブには、ブラウザが提供するデフォルトのアクションが 5 つあります(「進む」、「ページ情報」、「更新」、「ページ内検索」、「ブラウザで開く」)。さらに、最大 7 つまで追加できます。これらのメニュー項目は、アイコン行とブラウザ提供の項目の間に挿入されます。(下の画像を参照)。実際の数は、基盤となるブラウザの実装によって異なります。(たとえば、バージョン 117 では、メニュー項目が 5 つから 7 つに増えました)。そのため、最も重要なアイテムを最初に追加することをおすすめします。
カスタム アクションには、右上のその他メニューからアクセスできます。

メニュー項目を追加するには、タイトルと PendingIntent を指定して CustomTabsIntent.Builder.addMenuItem() を呼び出します。ユーザーがメニュー項目をタップすると、ブラウザは PendingIntent を呼び出します。
val intent = CustomTabsIntent.Builder()
// ...
.addMenuItem("Menu item 1", pendingIntent)
.addMenuItem("Menu item 2", pendingIntent)
.addMenuItem("Menu item 3", pendingIntent)
.addMenuItem("Menu item 4", pendingIntent)
.addMenuItem("Menu item 5", pendingIntent)
.build()
閉じるボタンをカスタマイズする
カスタムタブをアプリのフローに合わせるには、閉じるボタンをカスタマイズします。カスタムタブをモーダル ダイアログのように表示する場合は、デフォルトの “X” ボタンを使用します。カスタムタブをアプリフローの一部としてユーザーに感じさせたい場合は、戻る矢印を使用します。
val intentBuilder = CustomTabsIntent.Builder()
intentBuilder.setCloseButtonIcon(
BitmapFactory.decodeResource(resources, R.drawable.ic_arrow_back)
)
下部ツールバーを追加する
下部のツールバーは、カスタムタブに機能を追加する非常に柔軟な方法です。

RemoteViews オブジェクトを CustomTabIntent.Builder.setSecondaryToolbarViews() に渡すことで、下部のツールバーを完全にカスタマイズして動的に更新できます。
まず、新しいレイアウト ファイル res/layout/custom_tab_toolbar.xml を作成して、ツールバー レイアウトを宣言します。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ct_toolbar_next"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_margin="8dp"
android:padding="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Next" />
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ct_toolbar_previous"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_margin="8dp"
android:padding="8dp"
android:text="Previous" />
</LinearLayout>
次のステップでは、ツールバーの操作を処理する BroadcastReceiver を AndroidManifest.xml ファイルに登録します。
<application …>
<receiver android:name=".BottomToolbarBroadcastReceiver" />
</application>
次に、下部のツールバーとのすべてのインタラクションを処理する BroadcastReceiver を実装します。
class BottomToolbarBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val url = intent.dataString
val remoteViewId = intent.getIntExtra(CustomTabsIntent.EXTRA_REMOTEVIEWS_CLICKED_ID, -1)
if (remoteViewId == R.id.ct_toolbar_previous) {
// handle previous
} else if (remoteViewId == R.id.ct_toolbar_next) {
// handle next
}
}
}
最後に、ツールバーを登録します。
// Create the pending intent
val actionIntent = Intent(
this.applicationContext,
BottomToolbarBroadcastReceiver::class.java
)
val toolbarPendingIntent = PendingIntent.getBroadcast(
applicationContext,
0 /* request code */,
actionIntent,
PendingIntent.FLAG_MUTABLE
)
// Pass the toolbar layout to the RemoteViews instance
val secondaryToolbarViews = RemoteViews(packageName, R.layout.custom_tab_toolbar)
// All toolbar buttons
val clickableIds = intArrayOf(R.id.ct_toolbar_next, R.id.ct_toolbar_previous)
// Register the bottom toolbar when creating a new custom tab intent
val intent = CustomTabsIntent.Builder()
.setSecondaryToolbarViews(secondaryToolbarViews, clickableIds, toolbarPendingIntent)
.build()
ブックマーク ボタンとダウンロード ボタン
その他メニューのブックマーク ボタンとダウンロード ボタンは、デフォルトで有効になっています。無効にするには、CustomTabsIntent.Builder で次のコードを使用します。
val customTabsIntent = CustomTabsIntent.Builder()
.setBookmarksButtonEnabled(false)
.setDownloadButtonEnabled(false)
.build()
次のステップ: カスタムタブでウェブ コンテンツの読み込みを高速化する方法を学習する。