71

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.

img.setImageResource(R.drawable.ic_home);

ic_home is VectorDrawable and this code doesn't work.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Hamid Waezi
  • 878
  • 1
  • 7
  • 14

8 Answers8

148

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);  
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
Pramod B
  • 1,652
  • 1
  • 10
  • 7
26

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.

Jawnnypoo
  • 993
  • 12
  • 18
18

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.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
18

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

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_about"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
   app:srcCompat="@drawable/ic_vector_image"
    />

java

AppCompatImageView image = (AppCompatImageView) findViewById(R.id.iv_about);
image.setImageResource(R.drawable.ic_vector_image);

below code need to add in build.gradle

android { defaultConfig{ vectorDrawables.useSupportLibrary = true } }

And it will serve the perspective of app:srcCompat.

sanjeev kumar
  • 541
  • 4
  • 20
  • 5
    No need for that, according to the documentation "This will automatically be used when you use ImageView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views." So you can use just ImageView unless you are extending it. – Adrian Aug 08 '17 at 14:52
  • @user924 I didn't specify that you can use ImageView as long as the activity extends AppCompatActivity. If yours doesn't, it's like you said. – Adrian Oct 23 '17 at 10:38
3

for Java Code use:

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

and for XML use:

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/search_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        ads:srcCompat="@drawable/ic_barcode" //for Vector Image
        tools:ignore="VectorDrawableCompat" />
2

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 .

JJD
  • 50,076
  • 60
  • 203
  • 339
JSONParser
  • 1,112
  • 2
  • 15
  • 30
0

Use this:

android.support.v7.widget.AppCompatImageButton, 
android.support.v7.widget.AppCompatImageView,
android.support.v7.widget.AppCompatTextView 

instead of ImageButton, ImageView etc.

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

Garg
  • 2,731
  • 2
  • 36
  • 47
masoomyf
  • 685
  • 8
  • 15
-4

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

rakesh rajput
  • 606
  • 4
  • 5