Android Vector Drawables on Pre Lollipop Crash - Solution

Avoid Crash on pre-lollipop devices

Danish Amjad
AndroidPub

--

Without directly go for the solution first we need to clear the concept of what is vector drawables in android.

Vector Drawables:

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information? The major advantage of using a vector drawable is image scalability. It can be scaled without loss of display quality, which means the same file is resized for different screen densities without loss of image quality. This results in smaller APK files and less developer maintenance. You can also use vector images for animation by using multiple XML files instead of multiple images for each display resolution.

Recently I was developing the application I used vector drawables throughout in my application I set the vector drawable to textview so what happened when I used vector drawables on pre-lollipop. Everything works fine but when I use.

  • android:drawableLeft
  • android:drawableRight
  • android:drawableTop
  • android:drawableBottom

On ImageView, ImageButton, TextView & EditText.

Exception on Pre-lollipop:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/search_toggle.xml from drawable resource ID #0x7f0200a9

Solution:

Doing a lot of R&D I got the answer, we need to set the drawable on textView, imageView, imageButton & EditText programmatically. I created a function which sets the vector drawable without any issue. You can see the code below:

How to use it:

Just call out the function and pass it the parameters like the below:

So that’s it…. now you can see there will be no crash on pre-lollipop. Happy Coding :-)

If you want to learn more related to Android then check it out the below link:

Sharing Knowledge is Caring 😊

Thanks for reading this article. Be sure to clap to recommend this article if you found it helpful. It means a lot to me.

If you need any help then Join me on Twitter, Linkedin, Github, and Subscribe to my Youtube Channel.

--

--

Danish Amjad
AndroidPub

Senior Mobile Engineer (Android). Open Source Contributor, Technical Writer, ✉️ Dani.amjad12@gmail.com 🔗 https://danishamjad12.github.io