androidandroid-layoutandroid-drawableandroid-resourcesandroid-vectordrawable

Android - how to set tint on vector drawables with two or more colors


I love vector drawables and the fact that I can change it's tint in every layout with simple tint attribute, like this:

<ImageView
            android:id="@+id/image_payment_cash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@color/color1"
            android:src="@drawable/ic_cash" />

But I wonder if there is a way to specify two or more tints for the same vector (for multi-colored vectors)? So it can be set with something like this:

 <ImageView
            android:id="@+id/image_payment_cash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint1="@color/color1"
            android:tint2 = "@color/color2"
            android:src="@drawable/ic_cash" />

Solution

  • No, there is no way to tint multiple different colors of a Vector Drawable by default in XML.

    You can, in Java/Kotlin, inflate the drawable file to a VectorDrawable class programmatically, and then go through each layer and then change the colors individually.
    By using the same code, you could create a custom ImageView that had the "tint1" and "tint2" attributes.

    If you use Vector Drawables below API 21, be wary of the different classes used by the Android Framework to backport some of the VD elements.