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 trialLuís Nabais
3,229 PointsgetDrawable(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
3,229 PointsThanks 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
Nathan Wong
7,129 PointsThe "best" solution from the support library now is:
ContextCompat.getDrawable(context, R.drawable.your_drawable);
James Simshaw
28,738 PointsHello,
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
10,323 PointsThis 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
3,014 PointsIt 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.
James Simshaw
28,738 PointsThe 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.
Derek Markman
16,291 PointsThe 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
Courses Plus Student 18,780 PointsI 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
10,323 PointsWill 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
Courses Plus Student 18,780 PointsMy 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
10,323 PointsLines 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.
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsBen Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsWhen 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.