PhilLab PhilLab - 1 year ago 165
Android Question

Android Support Library: getDrawable independent if vector or not

When working with Android >= 5.0,

Drawable d = getResources().getDrawable(R.drawable.icon)

Correctly parses the XML and returns a valid drawable. But when using the new Vector Drawable Support Library (Version 23.4, Gradle 2.1.2), this code crashes under Android 4.



Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector

The solution would be to use

Drawable d = VectorDrawableCompat.create(getResources(), R.drawable.icon, null);

But this crashes if the resource is not a vector resource: Corrupt XML binary file

So what code has to be used instead of the first line so that it works with Android 4 and Android 6 and with vector and non-vector drawables - e.g. in all circumstances this line was used in an Android 5.0+ project? The support library article does not mention a way to perform this migration

Answer Source

I found the solution.

You need to add the support VectorDrawable in your activity manually.

try this in your activity:

static {

You should check this blog entry for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download