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

Luís Nabais
Luís Nabais
3,229 Points

getDrawable(int id) is deprecated in API 22

getDrawable(int id) is deprecated in API 22.

My search shows that now the same method requires more parameters, but if we use them, even as null, it says it requires API 21 at least.

What's your advice, to make sure it still works from API 14/15 to 22?

Thanks a lot.

6 Answers

Luís Nabais
Luís Nabais
3,229 Points

Thanks a lot for the fast answer, James.

After a few bit more of search, I found this usage, which is shown as the correct way of getting a drawable, and checks with what you say:

Drawable drawable = ResourcesCompat.getDrawable(getResources(), page.getImageId(), null);

It's here if anybody needs to consult it :)

Thanks again

Ben Attenborough
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 Points

When I try to use this ResourcesCompat shows up in red to indicate it 'cannot resolve symbol'. So it looks like this method is missing on my setup?

I've tired using Drawable drawable = getResources().getDrawable(page.getImageId(), getTheme());

But this causes a crash.

The "best" solution from the support library now is:

ContextCompat.getDrawable(context, R.drawable.your_drawable);

Hello,

My first thought for getting new functionality into older APIs was to see if there was something in the support library, which apparently there is, ResourcesCompat. The Android documentation is available at https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html though I haven't checked if there's just the v4 version or if there's other versions as well.

Edward C. Young
Edward C. Young
10,323 Points

Nathan Wong

This is the correct approach, but for those of you that are new to Gradle and imports:

Add

compile 'com.android.support:appcompat-v7:23.1.0'

to your build.gradle file, and replace the deprecated method with

Drawable drawable = ContextCompat.getDrawable(this, mCurrentPage.getImageId());

Po-Chun Hsu
Po-Chun Hsu
3,014 Points

It looks like we could apply the new API like this:

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

Android reference: public Drawable getDrawable (int id, Resources.Theme theme) Added in API level 21 ... theme The theme used to style the drawable attributes, may be null.

The problem with that is that your project would then only be compatible with API level 21 and up. If you want to be compatible with API 15, you need to use the ContextCompat(ResourcesCompat is apparently also being deprecated) method.

Ben Attenborough

The reason it cannot resolve the symbol is because you need to import the class. So just place your cursor over ResourcesCompat and hit alt + enter to import it.

Hope this helps.

Will Macleod
Will Macleod
Courses Plus Student 18,780 Points

I am doing this, buy I dont have an import option, I only get a 'create class' option, is this the same thing?

Thanks

Edward C. Young
Edward C. Young
10,323 Points

Will Macleod See my comment in Nathan Wong s answer. Not having compile 'com.android.support:appcompat-v7:23.1.0' in your build.gradle file will prevent the IDE from seeing that import should be available.

Will Macleod
Will Macleod
Courses Plus Student 18,780 Points

Edward C. Young

My build.gradle file shows this as the classpath ''com.android.tools.build:gradle:0.12.2'

Do i replace that with what you said? In which area in my build.gradle do I exactly paste it in? Under dependencies?

Thank you

Edward C. Young
Edward C. Young
10,323 Points

Lines 22 - 25 of the build.gradle file in the app directory should read:

22 dependencies {
23    compile fileTree(dir: 'libs', include: ['*.jar'])
24    compile 'com.android.support:appcompat-v7:23.1.0'
25 }

I can't get the markdown right, so I added the line numbers.