Im trying to use vector drawables on pre lollipop devices. I did all as instructed here but i still get this crash.
build.gradle:
The answers given here are ignoring a situation when you wish to add a drawable to a textview because it gives the same error. in my case I had
<TextView .... android:drawableLeft="some_vectore_drawable" />
I could not find how to solve this so I removed that line from the xml code and put it in my java code in this manner
Drawable somevectordrable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.somevectordrawable);
mytextview.setCompoundDrawableWithIntrinsicBounds(somevectordrable, null, null, null);
Clarification for the code,
Get the vector drawable from the drawables folder using AppCompatDrawableManager
Set the drawable we just got as the left drawable on our textview
The problem was that my activity wasn't extending AppCompatActivity
but regular Activity
.
This is not specified in any documentation/example for support vector drawables
Got this problem too when loading vectors from a selector on pre-lollipop devices:
Use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) in your onCreate method:
Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources. When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within android.graphics.drawable.DrawableContainer resources.
protected final void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
super.onCreate(savedInstanceState);
...
If you see yourself using <android.support.v7.widget.AppCompatImageView
when loading a vector drawable it might be a better idea to extend AppCompatActivity
instead of Activity
and go back to using regular <ImageView...app:srcCompat="@drawable/...
This is support-v4, appcompat-v7 library v23.2.0 bug it appears in API 19. @tim provide link to this bug issue.
You can upgrade to new library version 23.2.1 and bug is fixed now.
If you're having this issue with drawableLeft
/drawableStart
, etc. there is an easy solution, if you're using databinding.
Instead of:
android:drawableLeft="@drawable/somevector" ❌
Do:
android:drawableLeft="@{@drawable/somevector}" ✅
This works because databinding will generate code at compile time that retrieves the drawable in a compatible way.