Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setCloseButtonIcon() method doesn't change default Close button

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)

like image 508
tehnolog Avatar asked Jan 05 '16 14:01

tehnolog


2 Answers

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

like image 91
ade Avatar answered Sep 23 '22 11:09

ade


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.

like image 32
Joe Muller Avatar answered Sep 23 '22 11:09

Joe Muller