How to create Drawable from resource

AndroidDrawable

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Your Activity should have the method getResources. Do:

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


As of API version 21 this method is deprecated and can be replaced with:

Drawable myIcon = AppCompatResources.getDrawable(context, R.drawable.icon);

If you need to specify a custom theme, the following will apply it, but only if API is version 21 or greater:

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

Solution 2 - Android

This code is deprecated:

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

Use this instead:

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

Solution 3 - Android

The getDrawable (int id) method is deprecated as of API 22.

Instead you should use the getDrawable (int id, Resources.Theme theme) for API 21+

Code would look something like this.

Drawable myDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
	myDrawable = context.getResources().getDrawable(id, context.getTheme());
} else {
	myDrawable = context.getResources().getDrawable(id);
}

Solution 4 - Android

I would just like to add that if you are getting "deprecated" message when using getDrawable(...) you should use the following method from the support library instead.

ContextCompat.getDrawable(getContext(),R.drawable.[name])

You do not have to use getResources() when using this method.

This is equivalent to doing something like

Drawable mDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
    mDrawable = ContextCompat.getDrawable(getContext(),R.drawable.[name]);
} else {
    mDrawable = getResources().getDrawable(R.id.[name]);
}

This works on both pre and post Lollipop versions.

Solution 5 - Android

Get Drawable from vector resource irrespective of, whether its vector or not:

AppCompatResources.getDrawable(context, R.drawable.icon);

Note:
ContextCompat.getDrawable(context, R.drawable.icon); will produce android.content.res.Resources$NotFoundException for vector resource.

Solution 6 - Android

If you are trying to get the drawable from the view where the image is set as,

ivshowing.setBackgroundResource(R.drawable.one);

then the drawable will return only null value with the following code...

   Drawable drawable = (Drawable) ivshowing.getDrawable();

So, it's better to set the image with the following code, if you wanna retrieve the drawable from a particular view.

 ivshowing.setImageResource(R.drawable.one);

only then the drawable will we converted exactly.

Solution 7 - Android

If you are inheriting from a fragment you can do:

Drawable drawable = getActivity().getDrawable(R.drawable.icon)

Solution 8 - Android

You must get it via compatible way, others are deprecated:

Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.my_drawable, null);

Solution 9 - Android

In Kotlin you can do like this:

binding.floatingActionButton.setImageDrawable(
            AppCompatResources.getDrawable(this, android.R.drawable.ic_delete))

Where binding is a root View and this refer to Context, You need import

import androidx.appcompat.content.res.AppCompatResources

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUser7723337View Question on Stackoverflow
Solution 1 - AndroidJemsView Answer on Stackoverflow
Solution 2 - Androiddaniel kilinskasView Answer on Stackoverflow
Solution 3 - AndroidChris StillwellView Answer on Stackoverflow
Solution 4 - AndroidSameer KhanalView Answer on Stackoverflow
Solution 5 - AndroidDhaval PatelView Answer on Stackoverflow
Solution 6 - AndroidExceptionalView Answer on Stackoverflow
Solution 7 - Androidvorwerg-niView Answer on Stackoverflow
Solution 8 - AndroidN DroidevView Answer on Stackoverflow
Solution 9 - AndroidMoriView Answer on Stackoverflow