Blodhgard Blodhgard - 2 months ago 121x
Android Question

Android getResources().getDrawable() deprecated API 22

With new android API 22

is now deprecated.
Now the best approach is to use only

What change?


You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:

A) drawables without theme attributes

ResourcesCompat.getDrawable(getResources(),, null);

You'll get your unstyled drawable the old way. This is probably what you need.

Please note: ResourcesCompat.getDrawable() is not deprecated!

B) drawables with theme attributes


You'll obtain a styled Drawable as your Activity theme instructs.

EXTRA) drawables with theme attributes from another theme

ResourcesCompat.getDrawable(getResources(),, anotherTheme);


Android 21 (5.0 Lollipop) introduced some new theme attributes such as android:colorAccent that modify the appearance of drawables that hold references to those new theme attributes values.

The AppCompat library handles pre and post-Lollipop drawable styling for you.

If you do use the deprecated getDrawable() method to obtain a drawable resource with theme attributes, you will get a partially-styled drawable and a logcat warning. You can see this in API 22 android.content.res.Resources source code:

public Drawable getDrawable(int id) throws NotFoundException {
    final Drawable d = getDrawable(id, null);
    if (d != null && d.canApplyTheme()) {
        Log.w(TAG, "Drawable " + getResourceName(id) + " has unresolved theme "
                + "attributes! Consider using Resources.getDrawable(int, Theme) or "
                + "Context.getDrawable(int).", new RuntimeException());
    return d;

See ContextCompat Reference, ResourcesCompat Reference and Benjamin Weiss g+ post