How to set VectorDrawable as an image for ImageView programmatically

AndroidImageviewAndroid Vectordrawable

Android Problem Overview

I want to set some vectorDrawables to a ImageView in Android Studio.

I can set png and jpg drawable easily but when i want to set VectorDrawable, it does not work on imageview.


ic_home is VectorDrawable and this code doesn't work.

Android Solutions

Solution 1 - Android

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):


or by XML:


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:


Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

if you are concerned with the backward compatibility then you should use AppCompatImageView instead of image view. go through the code below.



AppCompatImageView image = (AppCompatImageView) findViewById(;

below code need to add in build.gradle

android { defaultConfig{ vectorDrawables.useSupportLibrary = true } }

And it will serve the perspective of app:srcCompat.

Solution 5 - Android

for Java Code use:

formate_img.setImageResource(R.drawable.ic_text);//ic_text is a Vector Image

and for XML use:

        ads:srcCompat="@drawable/ic_barcode" //for Vector Image
        tools:ignore="VectorDrawableCompat" />

Solution 6 - Android

I had a vector in recycler view I was using img.setImageResource(R.drawable.ic_home) which didn't worked properly like some other image get formed in some item of recycler view. Then I used img.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_home)) this worked .

Solution 7 - Android

Use this:,, 

instead of ImageButton, ImageView etc.

If vector type image is used. Mainly for custom views.

Solution 8 - Android

Delete two folders form your drawable folder then rebuild your project it will work properlyenter image description here


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHamid.WaeziView Question on Stackoverflow
Solution 1 - AndroidPramod BaggolliView Answer on Stackoverflow
Solution 2 - AndroidJawnnypooView Answer on Stackoverflow
Solution 3 - AndroidPriyank PatelView Answer on Stackoverflow
Solution 4 - Androidsanjeev kumarView Answer on Stackoverflow
Solution 5 - AndroidNull Pointer ExceptionView Answer on Stackoverflow
Solution 6 - AndroidJSONParserView Answer on Stackoverflow
Solution 7 - AndroidmasoomyfView Answer on Stackoverflow
Solution 8 - Androidrakesh rajputView Answer on Stackoverflow