araks araks - 10 months ago 80
Android Question

AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21

I have an AppWidget and I'd like to use VectorDrawables in it also on pre-Lollipop devices.
VectorDrawableCompat won't work with the RemoteViews I create.

To keep my app APK size down, I don't want to add alternative PNG versions of my drawables for older API platforms.

How can I do that?


UPDATE 05/09/2016

As noted by @kirill-kulakov in its answer, the latest updates of the Support Library restricted the TintContextWrapper visibility to its own package. I'm updating my answer to remove the incorrect code, but please thank Kirill for the correction!

VectorDrawable and RemoteViews pre-Lollipop

You can avoid adding alternative rasterized versions of your vector drawable resources with an easy hack: use AppCompat TintResources through TintContextWrapper using AppCompatDrawableManager.

TintResources AppCompatDrawableManager is the class that among other things, on pre-Lollipop devices, parses the VectorDrawables XML files and convert them into VectorDrawableCompat instances that can be used all the way down to API 7.

Then, once you have a VectorDrawableCompat instance, rasterize it onto a Bitmap. You'll later use this bitmap in a remote ImageView.

Before we begin: AppCompat Library

Ensure you are using Android Studio 2.0+ and have configured your app build.gradle file as follows:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true

dependencies {
  compile ''

Update your AppWidgetProvider

First of all: don't set your vector drawable resources inside your RemoteViews layout file (neither android:src nor app:srcCompat will work). You'll have to set them programmatically.

Inside your AppWidgetProvider class set the vector resource or a rasterized version depending on the API level:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);

  remoteViews.setImageViewResource(, R.drawable.vector);

} else {
  Drawable d = AppCompatDrawableManager.get().getDrawable(context, R.drawable.vector);
  Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(),
  Canvas c = new Canvas(b);
  d.setBounds(0, 0, c.getWidth(), c.getHeight());
  remoteViews.setImageViewBitmap(, b);