Tint VectorDrawable inside Drawable Resource xml Tint VectorDrawable inside Drawable Resource xml android android

Tint VectorDrawable inside Drawable Resource xml


You can define colors as attributes and reference them in your SVG. Then can load the drawable styled by a theme.

Short example:

attributes.xml

<declare-styleable name="vectorColors">    <attr name="primaryColor" format="color" />    <attr name="secondaryColor" format="color" /></declare-styleable>

ic_icon.xml

<path    android:fillColor="?attr/primaryColor"    android:pathData="...." /><path    android:fillColor="?attr/secondaryColor"    android:pathData="...." />

styles.xml

<style name="svgColors">    <item name="primaryColor">@color/someColor</item>     <!-- also can use #RRGGBB way -->    <item name="secondaryColor">#ff0000</item></style>

Then load your theme and drawable:

Resources.Theme theme = getResources().newTheme();theme.applyStyle(R.style.svgColors, false);VectorDrawableCompat drawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_icon, theme);imageView.setImageDrawable(drawable);

In your specific selector you should replace

<item>    <bitmap        android:src="@drawable/ic_icon"        android:tint="@color/white"/></item>

by <item android:drawable="@drawable/ic_icon">

Reference and full example