Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJ A
Full Stack JavaScript Techdegree Student 4,646 PointsWhat is the alternative for the deprecated getDrawable() function?
In API version 22, the getDrawable() function of android.content.res.Resources is now deprecated. What is the correct way to set an image view now?
My current code:
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
2 Answers
benjamind
6,442 PointsIn the interactive story app you can use this code:
// Old:
// Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
// New:
Drawable drawable = ResourcesCompat.getDrawable(getResources(), mCurrentPage.getImageId(), null);
mImageView.setImageDrawable(drawable);
cf: https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html
Kourosh Raeen
23,733 PointsYou can use the one from the ContextCompat
class:
ContextCompat.getDrawable(getActivity(), R.drawable.name);