setCloseButtonIcon() method doesn't change default Close button - android

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)

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

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.

Related

VectorDrawable image show not correct in the android.support.v7.preference.Preference

Android studio 3.6
Here VectorDrawable (res/drawable/ic_phone_bw.xml)
<vector android:height="24dp" android:viewportHeight="294.8"
android:viewportWidth="294.8" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M147.4,147.4m-141.73,0a141.73,141.73 0,1 1,283.46 0a141.73,141.73 0,1 1,-283.46 0"/>
<path android:fillColor="#FF000000" android:pathData="M147.4,147.4m-131.99,0a131.99,131.99 0,1 1,263.98 0a131.99,131.99 0,1 1,-263.98 0"/>
<path android:fillColor="#FFFFFF" android:pathData="M200.62,248.9H94.19c-1.39,0 -2.52,-1.13 -2.52,-2.52V49.48c0,-1.39 1.13,-2.52 2.52,-2.52h106.43c1.39,0 2.52,1.13 2.52,2.52v196.9C203.13,247.77 202.01,248.9 200.62,248.9z"/>
<path android:fillColor="#FF000000" android:pathData="M102.7,75.7h89.39v149.58h-89.39z"/>
</vector>
I want to use it in Preference like this:
But here result:
Why icon show not correct (black dot)?
For reference, here is a SVG equivalent of your VectorDrawable.
<svg width="240px" height="240px" viewBox="0 0 294.8 294.8">
<path fill="#FFFFFF" d="M147.4,147.4m-141.73,0a141.73,141.73 0,1 1,283.46 0a141.73,141.73 0,1 1,-283.46 0"/>
<path fill="#000000" d="M147.4,147.4m-131.99,0a131.99,131.99 0,1 1,263.98 0a131.99,131.99 0,1 1,-263.98 0"/>
<path fill="#FFFFFF" d="M200.62,248.9H94.19c-1.39,0 -2.52,-1.13 -2.52,-2.52V49.48c0,-1.39 1.13,-2.52 2.52,-2.52h106.43c1.39,0 2.52,1.13 2.52,2.52v196.9C203.13,247.77 202.01,248.9 200.62,248.9z"/>
<path fill="#000000" d="M102.7,75.7h89.39v149.58h-89.39z"/>
</svg>
Note that there are two black and two white paths in this icon.
There could be several things happening here:
There is something in the white third path that Android doesn't like (doesn't parse correctly), causing that path to not be drawn properly. This is unlikely.
I notice that the icons in your screenshots have a tint to them. They are grey rather than black. My guess is that you are tinting or setting the colour of the icon in a way that is setting all the paths to grey - including the white ones.
Assuming #2 is correct, thereare a couple of solutions:
Change the way you are recolouring the icons
Redraw this icon so that it consists of just one or two black paths. The parts that are white now, should instead just be holes in the black parts. Then you colour the icon, there will no longer be any white parts that get recoloured.

How to make polygonal vector artwork

I am trying to find a way to make
type of vector but I have no idea how to make it I have seen in many splash screen of the app any hint will be helpful .
You could create a <vector> drawable and paint a bunch of semi-transparent shapes. Here's an example:
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="360dp"
android:height="160dp"
android:viewportWidth="360.0"
android:viewportHeight="160.0">
<path
android:fillColor="#3000"
android:pathData="M0 0h360v160h-360z"/>
<path
android:fillColor="#3000"
android:pathData="M0 0h48L0 60z"/>
<path
android:fillColor="#3000"
android:pathData="M0 160V40L100 50L140 160z"/>
<path
android:fillColor="#3000"
android:pathData="M70 0L80 140L300 0z"/>
<path
android:fillColor="#3000"
android:pathData="M300 0L280 160H360V0z"/>
<path
android:fillColor="#3000"
android:pathData="M320 0L340 160H360V0z"/>
<path
android:fillColor="#3000"
android:pathData="M100 0L100 50L260 160H360V0"/>
<path
android:fillColor="#3000"
android:pathData="M0 160L80 140LL120 160"/>
</vector>
This is obviously not as beautiful as your attached image, but it shows that the general technique works to create an image like the one you posted.

Vector Drawable in the circle

is there an easy way to generate Vector Drawable that is a circle with the icon inside from the existing vector drawable?
Example:
I would suggest something like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:gravity="fill"
android:drawable="#drawable/ic_brightness_1_black_24dp"
/>
<item
android:gravity="center"
android:drawable="#drawable/ic_call_black_24dp"
android:top="20dp"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
/>
</layer-list>
The resources with ids ic_brightness_1_black_24dp and ic_call_black_24dp are imported vector drawables.
ic_brightness_1_black_24dp:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#303F9F"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>
and ic_call_black_24dp:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>
Actually it's quite simple, you just need to include both paths in a single vector, so with your paths it will look like the following:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#303F9F"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>
The result will obviously depend on sizes of paths in relation to each other, and since scaling them without a graphical tool is a pain, DimDim's solution with a layer-list is easier to implement.
Since nobody has mentioned how to do this using vector drawing as the question says here is the way to do it.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="144dp"
android:height="144dp"
android:viewportWidth="144"
android:viewportHeight="144">
<path
android:pathData="M72,72m-50,0a50,50 0,1 1,100 0a50,50 0,1 1,-100 0"
android:strokeWidth="9"
android:fillColor="#00000000"
android:strokeColor="#fff"/>
</vector>
M72,72m -> circle's center coordinates
50` -> the circle's radius
100` -> circle's diameter
strokeWidth -> the ring's thickness
To make it into a disc instead of a ring change the fillColor
To make your circle half the size change all occurrences of 50 to 25 and all occurrences of 100 to 50. Change accordingly for other sizes.
To move the circle around inside the viewport change the circle's coordinates (the 72 numbers)
These numbers obviously are related to the viewport size. 72 is the center for 144 which is the defined viewport size in this case. To center it in a 200 viewport size you would need to use 100
Use this way,
I tried in my own and it's working fine. It looks like this.
for button_round:

Chrome custom tabs - setCloseButtonIcon() not changing icon

I'm trying to change icon for close button in Chrome custom tabs. But it is not changing.
Here's my code:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
According to this documentation, the close icon needs to be 24dp x 24dp. Something like this works:
<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>
Old question, but I stumbled across this and recently had what I believe is the same problem, so I'll answer.
I believe the issue is that you are using a vector drawable, but your code expects a PNG. I ran into this issue when I used a debugging breakpoint to discover that the result of parsing the image file was null.
This answer helped me. Here's the most relevant code section from that answer. It uses Android KTX.
AppCompatResources.getDrawable(activity, R.drawable.ic_arrow_back_white_24dp)?.let {
builder.setCloseButtonIcon(it.toBitmap())
}

Vector Drawable not showing vertical line

I am creating drawable in xml using the vector. I am able to draw a rectangle using the path but when I am trying to draw a completely vertical or horizontal line its not showing.
Here is my code
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:strokeWidth="1"
android:strokeColor="#c1ae1e"
android:pathData="M0 0,H24,V24,H0,V0"/>
<path
android:strokeWidth="3"
android:strokeColor="#4c4c4c"
android:fillColor="#36352c"
android:pathData="M12 0,L12 24"/>
<path
android:strokeWidth="3"
android:strokeColor="#4c4c4c"
android:fillColor="#36352c"
android:pathData="M0 12,L24 12"/>
</vector>
And here is the preview output-
Try combining it into one path. I'm not sure why, but having a completely horizontal or vertical line with only two points will not render. Since I had to make a cross shape, I was able to get away with combining both the vertical or horizontal lines like so:
<path
android:strokeColor="#FF000000"
android:strokeWidth="0.5"
android:pathData="M14,0 l0,28 M0,14 l28,0"/>
This will also happen if you make an arc that calculates to be a straight like (not something most people would do, but I've seen it while changing arc values, and might be related as to why the lines do not show)
Give this a try:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:strokeWidth="1"
android:strokeColor="#c1ae1e"
android:pathData="M0 0,H24,V24,H0,V0"/>
<path
android:strokeWidth="3"
android:strokeColor="#4c4c4c"
android:fillColor="#36352c"
android:pathData="M12 0,L12 24 M0 12,L24 12"/>
</vector>

Categories

Resources