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 trial

Android Build an Interactive Story App (Retired) Finishing the User Interface Review: Adding Story Pages

J A
seal-mask
.a{fill-rule:evenodd;}techdegree
J A
Full Stack JavaScript Techdegree Student 4,646 Points

What 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

In 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
Kourosh Raeen
23,733 Points

You can use the one from the ContextCompat class:

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