How to create Drawable from resource


Question

I have a image res/drawable/test.png (R.drawable.test).
I want to pass this image to a function which accepts Drawable.
(e.g. mButton.setCompoundDrawables())

So how to convert a image resource to an Drawable?

1
269
2/19/2015 11:51:44 AM

Accepted Answer

Your Activity should have the method getResources. Do:

Drawable myIcon = getResources().getDrawable( R.drawable.icon );
542
1/27/2011 3:18:09 PM

This code is deprecated:

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

Use this instead:

Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon