96

I'm using vector drawables in android prior to Lollipop and these are of some of my libraries and tool versions:

  • Android Studio : 2.0
  • Android Gradle Plugin : 2.0.0
  • Build Tools : 23.0.2
  • Android Support Library : 23.3.0

I added this property in my app level Build.Gradle

android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
   }  
}

It is also worth mentioning that I use an extra drawable such as LayerDrawable(layer_list) as stated in Android official Blog (link here) for setting drawables for vector drawables outside of app:srcCompat

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

You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop. However, AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable. By using this indirection, you can use vector drawables in cases such as TextView’s android:drawableLeft attribute, which wouldn’t normally be able to support vector drawables.

When I'm using app:srcCompat everything works fine, but when I use:

android:background
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom

on ImageView, ImageButton, TextView or EditText prior to Lollipop, it throws an expection:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/search_toggle.xml from drawable resource ID #0x7f0200a9
Behzad Bahmanyar
  • 6,195
  • 4
  • 35
  • 41
  • Possible duplicate of [Android vector compatibility](http://stackoverflow.com/questions/31870992/android-vector-compatibility) – AdamHurwitz Jul 21 '16 at 20:47
  • To see how use VectorDrawable with drawableLeft, drawableRight, drawableTop, drawableBottom check out [This answer](http://stackoverflow.com/a/40250753/5079879) – Behzad Bahmanyar Oct 25 '16 at 22:30
  • Can you check my answer here? http://stackoverflow.com/a/40523623/2557258 – Yazon2006 Nov 16 '16 at 15:12
  • In my case, vactor drawables weren't in right package (open project view) orginal answer here http://stackoverflow.com/a/35836318/2163045 – murt Dec 05 '16 at 16:04

17 Answers17

112

LATEST UPDATE - Jun/2019

Support Library has changed a bit since the original answer. Now, even the Android plugin for Gradle is able to automatically generate the PNG at build time. So, below are two new approaches that should work these days. You can find more info here:

PNG Generation

Gradle can automatically create PNG images from your assets at build time. However, in this approach, not all xml elements are supported. This solution is convenient because you don't need to change anything in your code or in your build.gradle. Just make sure you are using Android Plugin 1.5.0 or higher and Android Studio 2.2 or higher.

I'm using this solution in my app and works fine. No additional build.gradle flag necessary. No hacks is necessary. If you go to /build/generated/res/pngs/... you can see all generated PNGs.

So, if you have some simple icon (since not all xml elements are supported), this solution may work for you. Just update your Android Studio and your Android plugin for Gradle.

Support Library

Probably, this is the solution that will work for you. If you came here, it means your Android Studio is not generating the PNGs automatically. So, your app is crashing.

Or maybe, you don't want Android Studio to generate any PNG at all.

Differently from that "Auto-PNG generation" which supports a subset of XML element, this solution, supports all xml tags. So, you have full support to your vector drawable.

You must first, update your build.gradle to support it:

android {
  defaultConfig {
    // This flag will also prevents Android Studio from generating PNGs automatically
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
  // Use this for Support Library
  implementation 'com.android.support:appcompat-v7:23.2.0' // OR HIGHER

  // Use this for AndroidX
  implementation 'androidx.appcompat:appcompat:1.1.0' // OR HIGHER
}

And then, use app:srcCompat instead of android:src while loading VectorDrawables. Don't forget this.

For TextView, if you are using the androidx version of the Support Library, you can use app:drawableLeftCompat (or right, top, bottom) instead of app:drawableLeft

In case of CheckBox/RadioButton, use app:buttonCompat instead of android:button.

If you are not using the androidx version of the Support Library and your minSdkVersion is 17 or higher or using a button, you may try to set programmatically via

Drawable icon = AppCompatResources.getDrawable(context, <drawable_id>);
textView.setCompoundDrawablesWithIntrinsicBounds(<leftIcon>,<topIcon>,<rightIcon>,<bottomIcon>);

UPDATE - Jul/2016

They re-enabled that VectorDrawable in
Android Support Library 23.4.0

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

Maybe, build.gradle setting is now obsolete and you just need to enable it in proper activities (however, need to test).

Now, to enable it, you must do:

public class MainActivity extends AppCompatActivity {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    ...
}

Original Answer - Apr/2016

I think this is happening because Support Vector was disabled in the latest library version: 23.3.0

According to this POST:

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 (ISSUE 205236). Using app:srcCompat and setImageResource() continues to work.

If you visit issue ISSUE 205236, it seems that they will enable in the future but the memory issue will not be fixed soon:

In the next release I've added an opt-in API where you can re-enable the VectorDrawable support which was removed. It comes with the same caveats as before though (memory usage and problems with Configuration updating).

I had a similar issue. So, in my case, I reverted all icons which use vector drawable from resource to PNG images again (since the memory issue will keep happening even after they provide an option to enable it again).

I'm not sure if this is the best option, but it fixes all the crashes in my opinion.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 1
    Thanks @Guilherme P. but why didn't you removed the `vectorDrawables.useSupportLibrary = true` peroperty to go back to enabling **generating pngs at build time** again? – Behzad Bahmanyar Apr 27 '16 at 19:58
  • Oh Sorry if I was not clear... I did not removed that tag from build.gradle.. I meant, I deleted all vectors_images.xml from drawable folder and replace them by PNG icons (like I was using in the past)... In my case, I was using vector drawable in place of some icons... – guipivoto Apr 27 '16 at 20:28
  • I know. but even though without the tag from build.gradle, pre lollipop androids should be able use vector drawables by generating pngs at build time? doesn't it? – Behzad Bahmanyar Apr 27 '16 at 20:32
  • 1
    That was disabled in latest version v23.3.0 due to memory issues. This way, they can not generate png at runtime... That's why in the logcat error they print: unknown tag vector (or something like that). – guipivoto Apr 27 '16 at 20:33
  • Thanks for explanation. in my case `srcCompat` is enough for most layouts and until a proper fix, I going to replace drawable_left ,right & ... with a `LinearLayout` and `ImageView`. – Behzad Bahmanyar Apr 27 '16 at 20:38
  • 4
    Here is the opt-in instructions https://plus.google.com/+AndroidDevelopers/posts/B7QhFkWZ6YX. Unfortunately VectorDrawables are still not working on Pre-Lollipop device. I'm on Support Library 23.4.0 and have both 'generatedDensities = []' and 'vectorDrawables.useSupportLibrary = true' called in defaultConfig{}. – AdamHurwitz Jul 21 '16 at 02:20
  • any luck with this? facing the same issue. I'm using 23.4.0 and have the option enabled...no joy – AhmedW Jul 22 '16 at 13:18
  • 1
    @AdamHurwitz I updated the answer.. It seems that it was enabled again.. However, you must enable it differently now. – guipivoto Jul 30 '16 at 18:30
  • @AhmedW I updated the answer.. It seems that it was enabled again.. However, you must enable it differently now. – guipivoto Jul 30 '16 at 18:30
  • @GuilhermeP - In order to have backwards compatibility for Vector Drawables here is the following configuration I used for API Level pre-20 devices. build.gradle: vectorDrawables.useSupportLibrary = true [new line] dependencies{classpath 'com.android.tools.build:gradle:2.1.2'} Activity: onCreate > AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); XML ImageView: app:srcCompat="@drawable/vector" – AdamHurwitz Jul 31 '16 at 21:00
  • To see how use VectorDrawable with drawableLeft, drawableRight, drawableTop, drawableBottom check out [This answer](http://stackoverflow.com/a/40250753/5079879) – Behzad Bahmanyar Oct 25 '16 at 22:31
  • Also don't forget to put your files into the "drawable" folder, rather than in "drawable-anydpi"! – Massimo Jun 30 '18 at 14:36
  • you might wanna try this approach as well from medium: https://android.jlelse.eu/android-vector-drawables-on-pre-lollipop-crash-solution-45c0c34f0160 – mochadwi Nov 15 '18 at 01:54
  • simply adding drawableRight programmatically solved my issue – BekaBot Jun 13 '19 at 05:12
  • Thanks a million for excellent explanation.I used app:drawableStartCompat insted of android:drawableStart and problem solved without adding vectorDrawables.useSupportLibrary = true to defaultConfig.Is it necessary to add it? – Zahra Dec 01 '19 at 14:12
  • @W0rmH0le , adding `implementation 'com.android.support:appcompat-v7:23.2.0'` won't do if you're using `androidx`, this is only if you're using the support library, right? – juztcode Jan 28 '20 at 03:37
  • 1
    @juztcode You're right. You should use 'androidx.appcompat:appcompat:1.1.0' – guipivoto Jan 29 '20 at 13:45
  • this answer has become very confusing, contains a lot of unnecessary information and does not provide a useful solution. – alex Oct 02 '20 at 08:53
  • I agree about the confusing due the updates. But nevertheless the magic was using `app:drawableLeftCompat` (or any other of that kind) I did not know about them. Thank you. – sebasira Dec 10 '20 at 21:56
65

I had the same problem. But doing a lot of R&D I got the answer.

For Imageview and ImageButton use,app:srcCompat="@drawable/...." and for other views like Button, Textview, instead of using "drawableLeft/right..." in the XML, specify drawables programmitically as :

button.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(mContext,R.drawable.ic_share_brown_18dp), null, null, null);

And use "AppCompatResources" to get the drawable.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shashank Kapsime
  • 799
  • 5
  • 10
60

To elaborate on the other very good answers, here is a diagram that can help you. It is valid if you have Support Library from 23.4.0 to at least 25.1.0.

VectorDrawable cheatsheet

Community
  • 1
  • 1
David Ferrand
  • 5,357
  • 1
  • 33
  • 43
39

The answer from Guillherme P is pretty awesome. Just to make a small improvement, you don't need to add that line in every activity, if you added it once in the Application class it will work as well.

public class App extends Application {

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

REMEMBER: You still need to have enabled the use of the support library in gradle:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

Also, make sure you are using a support library version greater than v23.4, when Google added back the support for Drawable Containers for VectorDrawables (release note)

Update

And for code changes:

  1. Make sure to update to app:srcCompat every place that accepts the android:src attribute (the IDE will warn you if it's invalid like for the <bitmap> tag).
  2. For drawableLeft, drawableStart, drawableRight, drawableEnd attributes used in TextView and similar views, you will have to set them programmatically for now. An example of setting drawableStart:

    Drawable drawable = AppCompatResources.getDrawable(
            getContext(),
            R.drawable.your_vector_drawable);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
    }
    
Benny
  • 1,650
  • 17
  • 20
  • remember that you need to have ON the use of the support library for vector drawables in gradle: vectorDrawables.useSupportLibrary = true – Benny Nov 22 '16 at 01:22
  • 2
    Yep. I know. But there is necessary a wrapper to make vector drawables work as drawables for TexViews, so this answer is incomplete. – cesards Nov 23 '16 at 09:56
  • 1
    Great answer. It is particularly good for apps with all of its activities extended from a custom base activity. – Hong Mar 01 '19 at 13:58
15

I had the same problem. And fix it by removing

vectorDrawables.useSupportLibrary = true

My target version is 25 and support library is

 compile 'com.android.support:appcompat-v7:25.3.1'
Rajesh N
  • 6,198
  • 2
  • 47
  • 58
11

VectorDrawables on pre-lollipop should work fine without using

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

If you want to use VectorDrawables inside ImageViews, you can use the attribute srcCompat and it will work, but inside Buttons or TextViews it won't, so you need to wrap the Drawable into an InsetDrawable or a LayerDrawable. There is another trick I discovered, if you are using data binding, you could do this:

android:drawableLeft="@{@drawable/vector_ic_access_time_24px}"
android:drawableStart="@{@drawable/vector_ic_access_time_24px}"

That will magically work, I haven't investigated what's happening behind the scenes, but I guess the TextView is using the getDrawable method from the AppCompatResources or similar.

cesards
  • 15,882
  • 11
  • 70
  • 65
  • How to set vector image in selector? – Tushar Gogna May 19 '17 at 11:11
  • after setting **vectorDrawables.useSupportLibrary = true** in gradle default and **AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);** in activity on create To avoid crash with **android:drawableleft in Textview**, set drawble left to the textview programitically for examlple: textview.setCompoundDrawablesWithIntrinsicBounds(R.drawable.movie, 0, 0, 0); – Afjalur Rahman Rana Mar 02 '19 at 17:42
9

Lot of R & d, finally getting this solution for crashes on pre-lollipop devices.

For Imageview

  • use app:srcCompat instead of android:src

For TextView/EditText

  • Remove drawableleft,drawableright.... and set from drawable java code.

txtview.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(EventDetailSinglePage.this, R.drawable.ic_done_black_24_n), null, null, null);

For Build.gradle

vectorDrawables.useSupportLibrary = true

Jatin Mandanka
  • 411
  • 11
  • 16
8

Easiest way use :

app:drawableRightCompat ="@drawable/ic_mobilelogin"
app:drawableEndCompat="@drawable/ic_mobilelogin"
app:srcCompat="@drawable/ic_mobile"

and... just use app:**Compatfor compatability. Also add support on build.gradle (module)

android {
   defaultConfig {
       vectorDrawables.useSupportLibrary = true
   }
}
JJD
  • 50,076
  • 60
  • 203
  • 339
Hamed Jaliliani
  • 2,789
  • 24
  • 31
5

For anyone who upgrade to android gradle 3.0 and above, there is no need to use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true), or set vectorDrawables.useSupportLibrary = true (add this will cause problem) and use app:srcCompat, it just works.

Take me two days to figure this out, and have not find any related docs in google's docs...

Geng Jiawen
  • 8,904
  • 3
  • 48
  • 37
  • Interesting, I'm using gradle 3.3.0 and this solution works. However, Android Studio is still telling me to enable set vectorDrawables.useSupportLibrary = true. – masterwok Jul 10 '18 at 03:46
3

After using the below code.

android {
  defaultConfig {
  vectorDrawables.useSupportLibrary = true  
                }
        }




public class App extends Application {
static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}}

still, vector images issue exists for below attributes are

DrawableEnd, DrawableStart, DrawableTop, DrawableBottom, Background

In this case, please follow as below, Instead of referencing vector image directly use selector tag as an intermediate drawable file.

Example:

ic_warranty_icon.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="17dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="17"
android:viewportHeight="24">

<path
    android:fillColor="#fff"
    android:pathData="M10.927,15.589l-1.549,0.355a7.47,7.47 0,0 1,-0.878 0.056c-4.136,0 -7.5,-3.364 -7.5,-7.5s3.364,-7.5 7.5,-7.5 7.5,3.364 7.5,7.5c0,3.286 -2.126,6.078 -5.073,7.089zM8.5,2a6.508,6.508 0,0 0,-6.5 6.5c0,3.583 2.917,6.5 6.5,6.5s6.5,-2.917 6.5,-6.5 -2.917,-6.5 -6.5,-6.5z" />

safe_ic_warranty_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_warranty_icon"  />
</selector>

Your TextView/Layout.

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/ic_warranty_icon"
       />


<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_warranty_icon"
       />
Rajesh Gr
  • 71
  • 3
  • Thanks !!! For background property, adding selector in between rather than directly using vector drawable worked for pre-lollipop api (19). – Ankur Nov 04 '19 at 07:12
  • in my case, this still doesn't work for API (16), even wrapping with `selector` – mochadwi Mar 31 '20 at 10:26
3

We tried 3 things

vectorDrawables.useSupportLibrary = true

Setting setCompatVectorFromResourcesEnabled in Application class

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

And use app:srcCompat

But even after that it was failing with

Resources$NotFoundException: File res/drawable/$my_icon__0.xml from color state list resource ID #0x7f080008

then we figured out that our SVG had a Gradient tag. Converting the gradient tag to individual paths for below API <= 23 and using the same SVG API >= 24 worked.

Got help from this answer https://stackoverflow.com/a/47783962/2171513

Aalap
  • 2,847
  • 2
  • 26
  • 24
  • 1
    I don't know why no one upvote this, but this could actually be the solution to my problem. Thank you – devmike01 May 21 '20 at 11:51
2

I am using VectorDrawables on Pre-lollipop devices and this is how I do it :-

Step 1: Put this in your app level gradle.

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

Step 2:

Put this in your Application class and don't forget to register your Application class in the manifest file.

public class App extends Application {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
}

Step 3:

Get VectorDrawables using,

imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.my_vector_drawable));
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51
1

I was struggling with this for hours.

I tried everything these answers told me to, but my app didn't stop crashing. I deleted this line: app:srcCompat="@drawable/keyboard" and my app stopped crashing. and then when I added this same thing back, it started crashing again. So I decided to open that file and I saw an error at the first line saying

"The drawable 'Keyboard' has no declaration in the base drawable folder; this can lead to crashes.

I right-clicked the file and clicked "Show in explorer" and it was not in the drawable folder but in the drawable-v24 directory. So I copied it and pasted to the drawable directory and finally got rid of crashes.

0

Simply overlap vector drawable to state-list then problem will be solved

For example you have back arrow vector image:

ic_back_arrow.xml

yes, you should overlap it to layer list xml (ic_back_arrow_vector_vector.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_back_arrow"/>
</layer-list>

Because logic:

vectorDrawables.useSupportLibrary = true

and

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

will not help you on the some China devices and older samsung devices. If you do not overlap them, it will fail.

SBotirov
  • 13,872
  • 7
  • 59
  • 81
0

In my case, I was using a TabLayout, which I configured as it :

TabLayoutMediator(tabLayout!!, viewPager!!) { tab, position ->
    if (position == 0)
        tab.icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_list, theme)
    else
        tab.icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_building_map, theme)
}.attach()

The app was crashing at line tab.icon = ...

I change these to tab.setIcon(R.drawable.my_vector_asset), as it :

TabLayoutMediator(tabLayout!!, viewPager!!) { tab, position ->
    if (position == 0)
        tab.setIcon(R.drawable.ic_list)
    else
        tab.setIcon(R.drawable.ic_building_map)
}.attach()

And it worked !

Mathieu
  • 1,435
  • 3
  • 16
  • 35
-2

Guilherme P's suggestion was not working for me. I went ahead and made the decision to use png's where I need to do things outside of app:srcCompat i.e. drawableLeft, drawableRight, etc. This was a pretty easy change to make, and doesn't have the potential memory issues AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); introduces.

Ryan Newsom
  • 696
  • 7
  • 12
-3

An alternative to Benny's answer is to create an Activity superclass:

public abstract class VectorDrawableActivity extends AppCompatActivity {
  static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  }

  //...
}

Now extend VectorDrawableActivity instead of AppCompatActivity.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268