Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,407 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