How to create Drawable from resource

User7723337 picture User7723337 · Jan 27, 2011 · Viewed 246.3k times · Source

I have an 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 can I convert an image resource to a Drawable?

Answer

Jems picture Jems · Jan 27, 2011

Your Activity should have the method getResources. Do:

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