How to set VectorDrawable as an image for ImageView programmatically

For those who want to load a vector drawable programmatically for other uses, such as setting a drawableLeft or otherwise, you can use:

Drawable drawable = AppCompatResources.getDrawable(context, drawableRes);

where the context is a AppCompatActivity.


As per official android developer blog, no changes for setImageResource() method at runtime for vectorDrawables.

If you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

For more details, check out this nice article AppCompat — Age of the vectors by Google Developer.


If you want to use vector drawables (less OR greater than API 21) just do the following:

Set the image programmatically (e.g. in your activity):

imageView.setImageResource(R.drawable.ic_left_arrow_blue); 

or by XML:

app:srcCompat="@drawable/your_vector_name"

In your app's build.gradle you need to include:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

And for vector support for less then API 21, add the following to onCreate:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);