Android: Alternative for context.getDrawable()

Bram picture Bram · Jan 6, 2015 · Viewed 68.6k times · Source

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().

Answer

user2417480 picture user2417480 · Aug 18, 2015

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.***)