Android: Alternative for context.getDrawable()

JavaAndroidEclipseAndroid Drawable

Java Problem Overview


I have used context.getDrawable() like this in my project:

Drawable greenProgressbar = context.getDrawable(R.drawable.custom_progressbargreen);

But Eclipse is giving me an error that it needs a Minimum API level of 21. This would mean after a quick google search my APP will only be usable on Android 5.0. Since not all devices are using this version of android I would like to have an alternative for context.getDrawable().

Java Solutions


Solution 1 - Java

The previously accepted method has been deprecated, according to the SDK 22 documentation:

> Prior to android.os.Build.VERSION_CODES#JELLY_BEAN, this function would not correctly retrieve the final configuration density when the resource ID passed here is an alias to another Drawable resource. This means that if the density configuration of the alias resource is different than the actual resource, the density of the returned Drawable would be incorrect, resulting in bad scaling.

As pointed out in this answer better solution would be to use ContextCompat: ContextCompat.getDrawable(context, R.drawable.***)

Solution 2 - Java

Add a getResources() after the context:

Drawable greenProgressbar = context.getResources().getDrawable(R.drawable.custom_progressbargreen);

Solution 3 - Java

I had a same situation which I wanted to reference getDrawable() method which is now deprecated.

What I used:

myButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_btn_off));

Solution 4 - Java

Drawable greenProgressbar = context.getResources().getDrawable(R.drawable.custom_progressbargreen);

Solution 5 - Java

AppCompatResources.getDrawable(context, R.drawable.*)

Solution 6 - Java

Solution for Kotlin programmers looks like:

val greenProgressbar = context!!.getDrawable(R.drawable.custom_progressbargreen)

or (from API 22)

val greenProgressbar = ContextCompat.getDrawable(R.drawable.custom_progressbargreen)

Solution 7 - Java

You should use " getDrawable(id, this.getTheme()) ". This method is not deprecated till now.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    view.setBackground(getResources().getDrawable(R.drawable.radioline,this.getTheme()));
} else {
   view.setBackground(getResources().getDrawable(R.drawable.radioline));
}

Solution 8 - Java

I agree using ContextCompact.getDrawable(Context context, int resID). It worked for me and my app targets API 19.

Solution 9 - Java

You can also set the resource directly without working with the drawable (Kotlin):

btn.setImageResource(R.drawable.ic_XXX)

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
QuestionBramView Question on Stackoverflow
Solution 1 - Javauser2417480View Answer on Stackoverflow
Solution 2 - Javaglm9637View Answer on Stackoverflow
Solution 3 - JavaUmandaView Answer on Stackoverflow
Solution 4 - Javauser4423334View Answer on Stackoverflow
Solution 5 - JavaNandoView Answer on Stackoverflow
Solution 6 - JavaJens van de MötterView Answer on Stackoverflow
Solution 7 - JavaMaulik BaraiyaView Answer on Stackoverflow
Solution 8 - JavaKaylen Travis PillayView Answer on Stackoverflow
Solution 9 - JavaLukas MohsView Answer on Stackoverflow