Android getResources().getDrawable() deprecated API 22
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Resources.getDrawable(int) was deprecated because it does not carry theme information in the newer API design. The practical replacement depends on your minimum SDK, but for most app code the safest answer is ContextCompat.getDrawable(context, id).
Why The Old Method Was Deprecated
The older call:
returns a drawable, but it ignores the more explicit theme-aware API shape introduced later. Android moved toward APIs that can resolve themed resources correctly and work more consistently across versions.
That is why the deprecation message points you away from the old no-theme overload.
The Common Replacement: ContextCompat.getDrawable
If you want one call that works well in app code across API levels, use ContextCompat.
This is the usual replacement in activities, fragments, adapters, and custom views.
It also makes the code easier to keep backward-compatible because the support library handles the version differences for you.
Theme-Aware Platform Replacement
If your minimum API level is already high enough and you want the platform API directly, use the theme-aware overload.
Or, from a Context:
But for most everyday app code, ContextCompat.getDrawable remains the most portable answer.
A Real Example In An Activity
This is the normal migration path from the deprecated call.
When AppCompatResources Is Better
If you are dealing with vector drawables or AppCompat resource behavior, AppCompatResources can be a better fit.
This is especially useful when the resource should be loaded through the AppCompat stack rather than only through the raw framework resource path.
Nullability Matters
All of these replacement APIs can return null if the resource cannot be resolved.
In Kotlin, this is even clearer:
Do not assume resource lookup can never fail, especially in library or dynamically themed code.
Migrating Existing Code
A practical migration rule is:
- replace
getResources().getDrawable(id)withContextCompat.getDrawable(context, id)in ordinary app code - use the theme-aware platform overload if you target newer API levels and want the platform call explicitly
- consider
AppCompatResources.getDrawablewhen AppCompat resource behavior matters
This is usually a simple mechanical change, but it is worth understanding why the change exists.
Common Pitfalls
The most common mistake is replacing the deprecated call with another API without considering whether theme resolution or AppCompat behavior matters.
Another mistake is using this where a different context is required, especially inside adapters or helper classes.
Developers also forget the return value can be null, which leads to avoidable crashes when resources are misconfigured.
Finally, if you are loading a vector drawable and the behavior still looks wrong, check whether AppCompatResources is the more appropriate loader for that code path.
Summary
- '
Resources.getDrawable(int)was deprecated in favor of theme-aware alternatives.' - '
ContextCompat.getDrawable(context, id)is the usual compatibility-friendly replacement.' - '
getResources().getDrawable(id, theme)is the direct theme-aware platform alternative.' - '
AppCompatResources.getDrawablecan be better for AppCompat-managed resources such as vector drawables.' - Treat the result as nullable and choose the loader that matches your resource stack.

