Home » Android » How to use vector drawables in Android API lower 21?

How to use vector drawables in Android API lower 21?

Posted by: admin February 23, 2018 Leave a comment

Questions:

I am working on an Android project and I chose <vector> to display icon because it is adaptable and dynamically, however, I just can run this app on devices running Android, which have API 21 or higher. My question is how can I use <vector> on lower Android version i.e. API 14 or kind of. Thanks!

<!-- drawable/ic_android_debug_bridge.xmlxml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:width="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/primaryColorDark"
    android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector>`
Answers:

Vector Drawables are now backward compatible, it’s just a matter of upgrading your gradle version to 1.4.0-beta3 or higher, and upgrade your IDE :

We are also excited to offer backwards compatibility for your vector
assets in Android Studio 1.4. Once you have a vectorDrawable image in
your res/drawable, the Gradle plugin will automatically generate
raster PNG images for API level 20 and below during build time. This
means you only need to update and maintain your vector asset for your
app project and Android Studio can take care of image conversion
process.

http://android-developers.blogspot.com.uy/2015/09/android-studio-14.html

Questions:
Answers:

With the support library 23.2, the true support for Vector Drawables has been provided all the way down to API v7.
It is recommended to disable the previous version of the support, which rendered PNG during build-time, by adding

// Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

to the build.gradle.

The implementation is fairly simple. There is a new srcCompat attribute on Drawables:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:srcCompat="@drawable/ic_add" />  <= this is new

Vector Drawables are also supported in cases like TextView’s drawableLeft property.

Source: library announcement

However, I would still recommend something like Iconics library, AndroidSVG, or another font-icon or SVG solution for full SVG-standand support.

Questions:
Answers:

VectorDrawable are supported pre-Lollipop via the Support Library, but the way to use them depends on the version of Support Library you have. And it may not work in all cases.

I’ve made this diagram to help (valid for Support Library 23.4.0 to – at least – 25.1.0).

VectorDrawable cheatsheet

Questions:
Answers:

you need use android Support Repository 30+ if you using android studio
and need android support library 23.2.1+ if using Eclipse.

check your build.gradle (project) if using version 2.0+ add below code in your build.gradle (app)

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

and: if using version 1.5 add below in your build.gradle(app)

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  

here is sample code for use vector icon:

<ImageView  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add"
  tools:ignore="MissingPrefix" />

or

<ImageButton
  android:layout_width="wrap_content"
  android:background="@android:color/transparent"
  app:srcCompat="@drawable/camera"
  tools:ignore="MissingPrefix"
  android:layout_height="wrap_content"/>

Vector Drawables are also supported in cases like TextView’s drawableLeft property. but it worked api 22+ to me and i still dont know how it will work for low api.

Also keep in mind if you want to be compatible below API 21:

  • you cannot use the android:background property in xml or View.setBackgroundResource() function. You need to use the View.setBackground().
  • you cannot use the svg-s in StateListDrawable xml-s or other xml drawables, you have to generate them programmatically.
  • you cannot use svg-s in case of notifications.
Questions:
Answers:

I found solution! For those who search solution with TextView and other “android” namespace attributes.
First of all this is necessary:

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

And in application class define this:

    @Override
    public void onCreate() {
        super.onCreate();
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

Now you can use app:srcCompat="@drawable/ic_add" but if you try to use android:background= or android:drawableLeft= it will crash app with “Error inflating” exception.

We can create wrapped drawable ic_add_wrapped.xml for this vector:

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

And now it will work with any property like drawableLeft or background. Just set android:drawableLeft="@drawable/ic_add_wrapped.xml". I’m very appreciate help from Sergey Zabelnikov who told me about this workaround: https://stackoverflow.com/users/4252851/sergey-zabelnikov

Questions:
Answers:

When you need to add SVG-image programatically, you can do it like this:

icon = VectorDrawableCompat.create(resources, R.drawable.ic_map_black_24dp, null)

Questions:
Answers:

I had an issue where my vector images would show up but would be completely black, this was due to an issue where you can’t reference color resources in the vector xml file.

So instead of @color/primaryColorDark you have to use the actual color e.g. #212121

Questions:
Answers:

Just to make @2Dee’s answer complete:

VectorDrawable cannot be used on API 20 and lower. The official method generates pngs, which then become BitmapDrawables, which breaks the idea of vector graphics.

Personally I prefer svg’s over xml vectors:

  • such graphics can be exported right from Illustrator or other popular software
  • svg supports transformations, text, masks and other things properly
  • true scallable vector graphics on all platforms
  • smaller size and faster builds

To use vector graphics you can try https://github.com/BigBadaboom/androidsvg . It’s an svg reader and svg-compatible ImageView.

Questions:
Answers:

If you are using Android Studio 3.0.0 you can set

android.enableAapt2=false 

in gradle.properties

https://www.reddit.com/r/androiddev/comments/6mj8di/android_studio_30_canary_6_released/