16

I try to change default icon for Close Button in Chrome custom tabs (CustomTabsIntent.Builder)

Simple code for testing:

Bitmap closeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
intentBuilder.setCloseButtonIcon(closeIcon);

But nothing happens. Why? (Nexus 7, Marshmallow)

tehnolog
  • 1,204
  • 1
  • 11
  • 23

2 Answers2

1

Typically this is caused by using a bitmap that has the 'wrong' dimensions. The correct dimensions are documented here: https://developer.android.com/reference/android/support/customtabs/CustomTabsIntent.html#KEY_ICON

ade
  • 4,056
  • 1
  • 20
  • 25
  • KEY_ICON != EXTRA_CLOSE_BUTTON_ICON so its unclear from the documentation. – TjerkW Jul 27 '16 at 15:25
  • i have a 48x48 png image which is doesn't take by the custom tabs. so i guess the documentation isn't applicable for the close button – herau Nov 14 '16 at 12:17
1

The close icon needs to be 24dp x 24dp. Something like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="?attr/colorControlNormal">
  <path
      android:fillColor="@android:color/white"
      android:pathData="M7.2,14.4m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
  <path
      android:fillColor="@android:color/white"
      android:pathData="M14.8,18m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
  <path
      android:fillColor="@android:color/white"
      android:pathData="M15.2,8.8m-4.8,0a4.8,4.8 0,1 1,9.6 0a4.8,4.8 0,1 1,-9.6 0"/>
</vector>

In Kotlin, you can retrieve this drawable and add it to your builder like this:

AppCompatResources.getDrawable(main, R.drawable.close_icon)?.let {
            DrawableCompat.setTint(it, Color.WHITE)
            builder.setCloseButtonIcon(it.toBitmap())
        }

This answer has some more details.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61