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!

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 Loading the First Page

I get the error get drawable is deprecated what should I change it to?

Drawable drawable = getResources().getDrawable(page.getImageId()); mImageView.setImageDrawable(drawable);

it says that getDrawable is deprecated, what should I do?

4 Answers

Seth Kroger
Seth Kroger
56,412 Points

Option: Do nothing.

"Deprecated" doesn't mean it won't work, but its use is discouraged moving forward. It won't prevent your app from compiling and building.

With this particular one, you'll have a problem trying to fix it because the fix is only valid for API level 21+ (Lollipop). You'll almost certainly get an error that it's not supported by your project's minimumSdkLevel (14 or 15).

Andrew Winkler
Andrew Winkler
37,739 Points

I was able to get mine working using ResourcesCompat. java Drawable drawable = ResourcesCompat.getDrawable(getResources(), page.getImageId(), null); mImageView.setImageDrawable(drawable);

thanks Andrew Winkler it is working

Evan Tidwell
Evan Tidwell
7,608 Points

Instead of

Drawable drawable = getResources().getDrawable(page.getImageId);

just use

Drawable drawable = getDrawable(page.getImageId());
Umberto D'Ovidio
Umberto D'Ovidio
4,563 Points

If you use Evan suggestion, make sure to surround you code code like this:

Drawable drawable = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            drawable = getDrawable(page.getImageId());
        } else {
            drawable = getResources().getDrawable(page.getImageId());

This will allow you to run the non-deprecated code if your user is using lollipop or some newer version of android, otherwise it will use the deprecated code