Android getResources().getDrawable() deprecated API 22

Blodhgard picture Blodhgard · Mar 13, 2015 · Viewed 392.8k times · Source

With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable().

What changed?

Answer

araks picture araks · Mar 19, 2015

You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:


A) drawables with theme attributes

ContextCompat.getDrawable(getActivity(), R.drawable.name);

You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need.


B) drawables without theme attributes

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable() is not deprecated!


EXTRA) drawables with theme attributes from another theme

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);