Get Drawable Of Vector Resource (with support library backward compatibility)

Vector Drawables Backward Compatibility support back to Android 2.1 (API level 7+).

Vector Resources is not supported for < API level 21, thus the Vector Drawables Backward Compatibility is required.

Module:app build.gradle

Edit Module:app build.gradle

  • include vectorDrawables.useSupportLibrary = true
  • include or
  • include

android {
  defaultConfig {
    applicationId ...
    vectorDrawables.useSupportLibrary = true

dependencies {
    // implementation ""
    // implementation ""
    implementation ""
    implementation ""


It's recommended to call AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) at Application.onCreate.

Sets whether vector drawables on older platforms (< API 21) can be used within DrawableContainer resources.

When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within DrawableContainer resources. You can then use those drawables in places such as android:src on ImageView, or android:drawableLeft on TextView.

This feature defaults to disabled, since enabling it can cause issues with memory usage, and problems updating Configuration instances. If you update the configuration manually, then you probably do not want to enable this. You have been warned.

Even with this disabled, you can still use vector resources through setImageResource(int) and its app:srcCompat attribute. They can also be used in anything which AppCompat inflates for you, such as menu resources.

Please note: this only takes effect in Activities created after this call.

If you don't do this, you might bump into android.content.res.Resources$NotFoundException: File res/drawable/ic_error_black_24dp.xml from drawable resource ID #0x7f08006a. If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way. See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info on older platforms (< API 21).

class MyApp : Application() {    override fun onCreate() {        super.onCreate()        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);    }}

NOTE: remember to load your application class in AndroidManifest.xml ->

Load vector drawable

Get vector Drawable programatically

val icon = AppCompatResources.getDrawable(context, R.drawable.ic_error_black_24dp)
val icon = ContextCompat.getDrawable(context, R.drawable.ic_error_black_24dp)

NOTE: AppCompatResources.getDrawable - This method supports inflation of and resources on devices where platform support is not available.

NOTE: Seems like AppCompatResources.getDrawable is more realiable for vector, but ContextCompat.getDrawable works when AppCompatDelegate.setCompatVectorFromResourcesEnabled is used.

imageView.setImageResource(context, R.drawable.ic_error_black_24dp)

NOTE: ImageView.setImageResource is equivelant to app:srcCompat in layout xml.

Load vector Drawable using app:srcCompat in layout.

  app:srcCompat="@drawable/ic_add_black_24dp" />
< xmlns:android=""

        app:srcCompat="@drawable/ic_add_black_24dp" />


Get vector Drawable from TypedArray for custom view

class BadgePreference : Preference {    private var badge: Drawable? = null    constructor(context: Context) : super(context) {}    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {        val a = context.theme.obtainStyledAttributes(                attrs,                R.styleable.PreferencesBadge,                0, 0)        try {            // Resources$NotFoundException            // badge = a.getDrawable(R.styleable.PreferencesBadge_badge)            val drawableResId = a.getResourceId(R.styleable.PreferencesBadge_badge, -1);            badge = AppCompatResources.getDrawable(context, drawableResId)        }        finally {            a.recycle()        }    }    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {    }}

Content of res/values/attrs.xml

    <declare-styleable name="PreferencesBadge">
        <attr name="badge" format="reference" />

Specify custom drawable attribute in layout.

<?xml version="1.0" encoding="utf-8"?>

        android:title="Export as Text"
        android:summary="Export as data as plain text file. Keep the file safe as it is not encrypted"



❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.