How to Use Vectordrawables in Android API Lower Than 21

How to use VectorDrawables in Android API lower than 21?

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

Why do API 19 devices (and other devices below API 21) need vectorDrawables.useSupportLibrary to load icons?

The reason for this is that native (as in, built into the Android framework) support for vector drawables was added in API 21 (Android 5.0 Lollipop).

The vectorDrawables.useSupportLibrary option enables a backward-compatibility feature in the Android support library (now known as AndroidX AppCompat) that allows it to load vector drawables at Runtime via the VectorDrawableCompat and AnimatedVectorDrawableCompat classes.

It is also possible to configure the Android Gradle Plugin to convert the vector images to PNG files at build time for use on pre–API 21 devices, but this defeats many of the benefits of using vector drawables in the first place, such as smaller app size.

Getting app:drawableEndCompat instead of android:drawableEnd warning for Lollipop API 21 devices

The primary motive behind this warning is to make your VectorDrawables look same on all devices by making them backward compatible.
By using "Compat", you would be ensuring that using your vector asset would not crash your app for devices below API 21(Lollipop). In short, using drawableEndCompat would allow anyone to use the same features of drawableEnd on older APIs(<21).
Now you would be thinking that what should I choose:
If you are using drawableEndCompat, it would work as you expect in every device. Devices with API more than 21 would internally unwrap them as normal drawableEnd as far as I know.
If you choose to use drawableEnd, it will be working for API 21 and above only.

If you think that I don't need any Compat Support : You may increase your application's minimum SDK from current value to atleast 21. Then you can use keyword drawableEnd peacefully without any warning.
Also, you may opt to create different layout file for different APIs under which in layout file for below API 21, use drawableEndCompat and for API 21 and above use drawableEnd. In my opinion, you can also check whether you can use both attributes at same place. I feel like that they can work together as well.

If you are not supporting devices below API 21, there is no problem: You should have no issues/warning. Also, one more thing I need to tell you - Android Studio sometimes throw warnings or bugs; even if you are right. In that case, if you feel that you were correct, you should try invalidate/restart option after clicking File option in Menu Bar. I would also suggest to try restarting your system to everyone who are using system for long durations or always keeping it on sleep mode.

So, for a TextView, you should use app:drawableEndCompat (or start, top, bottom) instead of app:drawableEnd

Unable to add drawable start with SVG to TextView for Android API lowers that 21

Add vectorDrawables.useSupportLibrary = true in you app level build.gradle as follows:

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

Refer to this guide.

How to properly use backwards compatible Vector Drawable with the latest Android Support Library?

Add the latest support lib to your app's build.gradle dependencies:

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

and add the following line in the same file:

android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
...
}

Import vector image through Vector Asset Studio.

That's all, you are ready to go!


ImageView

XML

Use app:srcCompat attribute instead of android:src:

<ImageView
...
app:srcCompat="@drawable/your_vector"
... />

Programmatically

Directly from resource id:

imageView.setImageResource(R.drawable.your_drawable);

Set as Drawable object (e.g. for tinting):

Drawable vectorDrawable 
= AppCompatResources.getDrawable(context, R.drawable.your_vector);
imageView.setImageDrawable(vectorDrawable);

And if you want to set tint:

DrawableCompat.setTint
(vectorDrawable, ContextCompat.getColor(context, R.color.your_color));

TextView drawable

XML

There is no simple solution: XML attribute android:drawableTop(Bottom etc) can't handle vector images on pre-Lollipop. One solution is to add initializer block to activity and wrap vector into another XML drawable. Second - to define a custom TextView.

Programmatically

Setting resource directly doesn't work, you have to use Drawable object. Get it the same way as for ImageView and set it with the appropriate method:

textView.setCompoundDrawablesWithIntrinsicBounds(vectorDrawable, null, null, null);

Menu icon

There is nothing special:

<item
...
android:icon="@drawable/your_vector"
... />

menuItem.setIcon(R.drawable.your_vector);

Notifications:

It's impossible, you have to use PNGs :(



Related Topics



Leave a reply



Submit