180

I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* package?

for example if drawableId was android.R.drawable.ic_delete

mContext.getResources().getDrawable(drawableId)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Blaskovicz
  • 6,122
  • 7
  • 41
  • 50

6 Answers6

247
Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(d);
030
  • 10,842
  • 12
  • 78
  • 123
Pete Houston
  • 14,931
  • 6
  • 47
  • 60
  • I also found that using the application context seems to work, thanks. – Blaskovicz Oct 19 '11 at 02:21
  • 26
    As of API 22. `getDrawable(int id)` is deprecated. Use `getDrawable(int id, Resources.Theme theme)` instead. The method `getTheme()` should be helpful. – Isaac Zais Apr 17 '15 at 18:07
  • 1
    I have a small doubt .In this code "The method getDrawable(int) from the type Resources is deprecated". According to one SO answer 1. Is it wrong to use Deprecated methods or classes in Java? From the definition of deprecated: "A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists." What is better alternative for this. – Shubham AgaRwal Aug 05 '15 at 13:42
119

As of API 21, you should use the getDrawable(int, Theme) method instead of getDrawable(int), as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null).

You should use the following code from the support library instead:

ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_email)

Using this method is equivalent to calling:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}
Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • `context.getDrawable(id);` seems to be equivalent to `resources.getDrawable(id, context.getTheme());` – ErickBergmann Mar 07 '17 at 02:46
  • This can be done in one line if you have the support library: `ResourcesCompat.getDrawable(resources, id, context.getTheme());` – k2col Jul 13 '17 at 13:47
15

As of API 21, you could also use:

   ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

Instead of ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_email)

Zain
  • 2,336
  • 1
  • 16
  • 25
8

From API 21 `getDrawable(int id)` is deprecated

So now you need to use

ResourcesCompat.getDrawable(context.getResources(), R.drawable.img_user, null)

But Best way to do is :

- You should create one common class for getting drawable and colors because if in future have any deprecation then you no need to do changes everywhere in your project.You just change in this method
import android.content.Context
import android.graphics.drawable.Drawable
import androidx.core.content.res.ResourcesCompat

object ResourceUtils {
    fun getColor(context: Context, color: Int): Int {
        return ResourcesCompat.getColor(context.resources, color, null)
    }

    fun getDrawable(context: Context, drawable: Int): Drawable? {
        return ResourcesCompat.getDrawable(context.resources, drawable, null)
    }
}

use this method like :

Drawable img=ResourceUtils.getDrawable(context, R.drawable.img_user)
image.setImageDrawable(img);
David Wheatley
  • 426
  • 6
  • 16
Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78
5

The best way is

 button.setBackgroundResource(android.R.drawable.ic_delete);

OR use the below code for Drawable left, top, right, bottom. I'm setting drawable left in this case.

int imgResource = R.drawable.left_img;
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);

and

getResources().getDrawable() is now deprecated

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
3

Following a solution for Kotlin programmers (from API 22)

val res = context?.let { ContextCompat.getDrawable(it, R.id.any_resource }