How to use vector drawables in Android API lower 21?

With the support library 23.2, the true support for Vector Drawables has been provided all the way down to API v7. It is recommended to disable the previous version of the support, which rendered PNG during build-time, by adding

// Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

to the build.gradle file.

The implementation is fairly simple. Just use the new srcCompat attribute on Drawables:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:srcCompat="@drawable/ic_add" />    <= this is new

Vector Drawables are also supported in cases like TextView's drawableLeft property.

Source: library announcement

However, I would still recommend something like Iconics library, AndroidSVG, or another font-icon or SVG solution for full SVG-standand support.


VectorDrawable are supported pre-Lollipop via the Support Library, but the way to use them depends on the version of Support Library you have. And it may not work in all cases.

I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).

VectorDrawable cheatsheet


I found solution! For those who search solution with TextView and other "android" namespace attributes. First of all this is necessary:

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

And in application class define this:

    @Override
    public void onCreate() {
        super.onCreate();
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

Now you can use app:srcCompat="@drawable/ic_add" but if you try to use android:background= or android:drawableLeft= it will crash app with "Error inflating" exception.

We can create wrapped drawable ic_add_wrapped.xml for this vector:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_add"/>
</layer-list>

And now it will work with any property like drawableLeft or background. Just set android:drawableLeft="@drawable/ic_add_wrapped.xml". WARNING! THIS IS A WORKAROUND SOLUTION. So you use it for your own risk.