Android
Android API
App Development
Deprecated Methods
GetResources().getDrawable()

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:

java
getResources().getDrawable(R.drawable.icon)

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.

java
1import android.graphics.drawable.Drawable;
2import androidx.core.content.ContextCompat;
3
4Drawable icon = ContextCompat.getDrawable(this, R.drawable.icon);

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.

java
Drawable icon = getResources().getDrawable(R.drawable.icon, getTheme());

Or, from a Context:

java
Drawable icon = getDrawable(R.drawable.icon);

But for most everyday app code, ContextCompat.getDrawable remains the most portable answer.

A Real Example In An Activity

java
1import android.os.Bundle;
2import android.widget.ImageView;
3import androidx.appcompat.app.AppCompatActivity;
4import androidx.core.content.ContextCompat;
5
6public class MainActivity extends AppCompatActivity {
7    @Override
8    protected void onCreate(Bundle savedInstanceState) {
9        super.onCreate(savedInstanceState);
10        setContentView(R.layout.activity_main);
11
12        ImageView imageView = findViewById(R.id.imageView);
13        imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.example_image));
14    }
15}

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.

java
1import android.graphics.drawable.Drawable;
2import androidx.appcompat.content.res.AppCompatResources;
3
4Drawable icon = AppCompatResources.getDrawable(this, R.drawable.ic_arrow);

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.

java
1Drawable icon = ContextCompat.getDrawable(this, R.drawable.icon);
2if (icon != null) {
3    imageView.setImageDrawable(icon);
4}

In Kotlin, this is even clearer:

kotlin
val icon = ContextCompat.getDrawable(this, R.drawable.icon)
imageView.setImageDrawable(icon)

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) with ContextCompat.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.getDrawable when 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.getDrawable can be better for AppCompat-managed resources such as vector drawables.'
  • Treat the result as nullable and choose the loader that matches your resource stack.

Course illustration
Course illustration

All Rights Reserved.