I've read answers and blog posts explaining VectorDrawables in Android and how they can be used instead of PNG files of different pixel densities.
I've seen that there is an android:tint
XML attribute that can be used on ImageButton
s and similar View
s, but I want to be able to apply a tint to vector icons I use as menu items, as you are unable to use android:tint
on menu items.
One blog post explained that tinted drawables can be created like so:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_action_something"
android:tint="@color/color_action_icons_tint"/>
where the XML file above is the tinted drawable, the referenced drawable through src
is the original vector (black), and the tint
is the colour that the icon will be tinted to.
However, the above did not work for me, giving me the following error:
android.content.res.Resources$NotFoundException: File res/drawable/ic_chevron_left_white_24dp.xml from drawable resource ID #0x7f02007e
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3735)
at android.content.res.Resources.loadDrawable(Resources.java:3603)
at android.content.res.Resources.getDrawable(Resources.java:1852)
at android.content.Context.getDrawable(Context.java:408)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:352)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at ...
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <bitmap> requires a valid src attribute
at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:761)
at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:726)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1150)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1063)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3719)
at android.content.res.Resources.loadDrawable(Resources.java:3603)
at android.content.res.Resources.getDrawable(Resources.java:1852)
at android.content.Context.getDrawable(Context.java:408)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:352)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at ...
Edit: This is my drawable ic_chevron_left_white_24dp.xml
:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_chevron_left_black_24dp"
android:tint="@color/white"/>
Both the above file, and the referenced one exist.
Furthermore, with the above, my IDE (Android Studio) displays this warning:
Rendering Problems : requires a valid 'src' attribute
This leads me to the question - how can I tint an XML vector drawable menu icon?
I am aware I can create a copy of the vector and change the fillColor
attribute to the colour I want, but apart from this, is there a way to do it non-programmatically?