VectorDrawable - is it available somehow for pre-Lollipop versions of Android?

AndroidGradleVectorAndroid Vectordrawable

Android Problem Overview


Background

I've noticed that Android now supports some kind of vector drawing, via a class called "VectorDrawable" (and also AnimatedVectorDrawable, BTW). I've found about it by looking at what's new on Android-Studio.

I wonder if this would be the end of having to put multiple files into multiple folders (mdpi, hdpi, xhdpi, etc). That would be great and might minimize apps sizes on some cases.

The questions

I'd like to ask a few questions regarding this new class:

  1. Is it possible to use it in older Android versions, maybe via a library of even the support library of Google?

  2. I'm not familiar with how it works, but can Lollipop handle SVG files? Can it do anything that is achievable on SVG files?

  3. Is there any sample/tutorial/video of using it, other than the documentation I've found?

Android Solutions


Solution 1 - Android

UPDATE ON March 2016

By Android Support Library 23.2.1 update, Support Vector Drawables and Animated Vector Drawables. (you can also use latestone for the same)

Please update version of a library in gradle file.

compile 'com.android.support:recyclerview-v7:23.2.1'

Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and animated-vector-drawable. new app:srcCompat attribute to reference vector drawables .

Check source on github with some sample examples.

Changes for v7 appcompat library:

Reverted dependency on vector assets so that developers using the appcompat library are not forced to use VectorDrawable and its associated build flags.

Solution 2 - Android

Update 2: They enable it again in 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.

Check this 23.4.0 available now

Update: This doesn't work from version 23.3.0 Check here for details. Proxy drawables don't work. app:srcCompat and setImageResource() work, however.


Vector Drawable support is available from the Support Library of version 23.2 and beyond. However, to properly use those drawables, they must be referenced indirectly.

First step would be to bump the AppCompat version.

compile 'com.android.support:appcompat-v7:23.2.0'

Second enable Vector Drawable support. If using Gradle plugin, 2.0+

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
   }  
}

Otherwise

android {  
   defaultConfig {  
     generatedDensities = []  
   }  

   aaptOptions {  
     additionalParameters "--no-version-vectors"  
   }  
}

Third, refer to the linked answer.

Solution 3 - Android

You can try this support library. It supports VectorDrawable and AnimatedVectorDrawable introduced in Lollipop with fully backwards compatibility.

Solution 4 - Android

To complement some of the answers here: yes, you can get support for VectorDrawables pre-Lollipop, at least partial.

How partial? It depends - I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).

VectorDrawable cheatsheet

Solution 5 - Android

Unfortunately, at this point of time VectorDrawable and AnimatedVectorDrawable are not available in support library. But to avail this feature in Pre-Lollipop versions, you can use the unofficial backport called MrVector.

MrVector is available in Github and it will support android versions 7+.

From the official Readme

To add MrVector dependency add the following line to your build.gradle dependencies block.

compile 'com.telly:mrvector:0.2.0'

To create the drawable from the vector XML,

Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_android);

Hope this helps.

Solution 6 - Android

Lollipop cannot handle SVG files without third-party libs.

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+.

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version

Links point to readmes, which provide enough information on how to use the lib and the converter.

Solution 7 - Android

If you are using VectorDrawable, Android Studio will automatically generate according PNG files (based on your XML files) for Pre-Lollipop versions.

Note that those generated PNG files are considered BitmapDrawables instead of VectorDrawables on devices running API below 21 and therefore can't be animated or similar on those devices.

See "backwards compatibility" for further details: http://android-developers.blogspot.co.at/2015/09/android-studio-14.html

Solution 8 - Android

There are no VectorDrawables in the support library at this time.

Funkystein is right -- VectorDrawable is similar to SVG, only supporting the features of vector drawing that are in highest demand so that android can focus on performance. pathData, for example has the same format as SVG's "d" string.

Solution 9 - Android

The great news is that Google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables!

But thanks go to the people who ported this library before Google!

> This is where the AppCompat libraries are great, they can bring many > of the new features of Android back to much earlier versions. With the > newly implemented VectorDrawable class, developers can now use vector > images all the way back to API 7 (Android 2.1 Eclair). Animated > vectors are a bit more limited, going only as far back as API 11 > (Android 3.0 Honeycomb), but that still encompasses more than 97% of > devices in active use today

Guide to use:

Refer "age-of-the-vectors" by @chrisbanes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionandroid developerView Question on Stackoverflow
Solution 1 - AndroidAmit VaghelaView Answer on Stackoverflow
Solution 2 - AndroidrazzledazzleView Answer on Stackoverflow
Solution 3 - AndroidV_JView Answer on Stackoverflow
Solution 4 - AndroidDavid FerrandView Answer on Stackoverflow
Solution 5 - AndroidgnuanuView Answer on Stackoverflow
Solution 6 - AndroidA-studentView Answer on Stackoverflow
Solution 7 - AndroidMehlyficationView Answer on Stackoverflow
Solution 8 - AndroidGeorge MountView Answer on Stackoverflow
Solution 9 - AndroidLOG_TAGView Answer on Stackoverflow