Change vector drawable color in item of layer-list

Viewed 412

I have the following drawable layer-list:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/grey" />
            <size
                android:width="64dp"
                android:height="64dp" />
        </shape>
    </item>
    <item
        android:layout_gravity="center_vertical"
        android:width="36dp"
        android:height="36dp"
        android:drawable="@drawable/ic_android_black_24dp"
        android:gravity="center"
        android:tint="@color/red" />
</layer-list>

where ic_android_black_24dp is a vector drawable.

I need to change the color of this drawable in xml (not programmatically) and without creating copy with the different color.

I tried android:tint, android:color and different approaches with <shape>.

Are there any solutions to do this?

1 Answers

Open file of this vector ic_android_black_24dp from drawable you will see lines like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24">

you need to add this line of code android:tint="@color/someColor" where the color of this vector will change as you want

the code will be

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24"
        android:tint="@color/someColor">
Related