5

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 ImageButtons and similar Views, 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?

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • You probably were missing a file... The ResourceNotFound Exception is easy to fix if you have the file that it says you need – OneCricketeer Jul 12 '16 at 12:08
  • @cricket_007 See my edit. – Farbod Salamat-Zadeh Jul 12 '16 at 12:35
  • @cricket_007 The white drawable you say is missing, but I have shown what it is in my edit. I reference the black drawable in the white drawable file, and the black one also exists. – Farbod Salamat-Zadeh Jul 12 '16 at 12:40
  • Have you read this? https://developer.android.com/training/material/drawables.html#DrawableTint – OneCricketeer Jul 12 '16 at 12:47
  • @cricket_007 Yes, but as I explained in my question, I can't seem to be able to use the `android:tint` attribute on menu items. How would I tint a vector drawable in XML for a menu icon? – Farbod Salamat-Zadeh Jul 12 '16 at 12:50
  • Have you tried a non-menu drawable? Like using a ImageView somewhere? I'm still not convinced your files (the XML and the image are in the drawable folder) according to the error. Can you show a screeenshot of that folder? Also add the menu xml where you reference the XML drawable – OneCricketeer Jul 12 '16 at 12:56
  • Additionally, you may want to read this. http://stackoverflow.com/questions/9788726/using-drawable-resources – OneCricketeer Jul 12 '16 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117096/discussion-between-farbod-salamat-zadeh-and-cricket-007). – Farbod Salamat-Zadeh Jul 12 '16 at 13:04
  • @cricket_007 Let us continue this discussion in chat ^ – Farbod Salamat-Zadeh Jul 12 '16 at 13:28
  • I have the same issue. Please consider starring https://issuetracker.google.com/issues/128665405 – Mark Apr 03 '19 at 11:49
  • And also this issue for making it possible to tint vector drawables in XML: https://issuetracker.google.com/issues/62369594 – Mark Apr 03 '19 at 11:50

1 Answers1

0

This is currently not possible. Please consider starring this feature request in the Android Issue Tracker.

Mark
  • 7,446
  • 5
  • 55
  • 75