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 a Weather App (2015) Hooking Up the Model to the View Setting the Weather Icon

AS has ".getDrawable" crossed out. It says it is deprecated.

When declaring the drawable, AS has ".getDrawable" crossed out. It says it is depreciated. Is there a different method to accomplish this for newer APIs? Or should this one be used for compatibility?

Jordan Cox
Jordan Cox
8,930 Points

Hi you make it sound so obvious, but unfortunately I am a NOOB literally copying line for line this lesson with absolutely no skills or knowledge whatsoever. Can you give the correct code please?

Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId()); mIconImageView.setImageDrawable(drawable);

the ".getDrawable" is crossed out with the depreciated message.

Thanks for any help!!

4 Answers

Hello,

You can use ContextCompat's getDrawable from the support library and pass a null in as the second parameter. More info on the deprecation and what to do can be found on this Google+ post here.

Jordan Cox
Jordan Cox
8,930 Points

Hi you make it sound so obvious, but unfortunately I am a NOOB literally copying line for line this lesson with absolutely no skills or knowledge whatsoever. Can you give the correct code please?

Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId()); mIconImageView.setImageDrawable(drawable);

the ".getDrawable" is crossed out with the depreciated message.

Thanks for any help!!

Change

Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId());

to

Drawable drawable = ContextCompat.getDrawable(this, mCurrentWeather.getIconId());

You're passing this in as the first parameter because it is requiring a context, which if you're in an Activity, the Activity is the context. Once you change that, optimize your imports and it should work. If not, let me know and I can help troubleshoot some more.

Cristian Daniel Marquez Barrios
Cristian Daniel Marquez Barrios
8,900 Points

My solution for this was:

Drawable drawable = ResourcesCompat.getDrawable(getResources(), mCurrentWeather.getIconId(), null);
mIconImageView.setImageDrawable(drawable);
Gavin Ralston
Gavin Ralston
28,770 Points

Which is fine, because you're not looking to import a themed drawable, just the resource itself.

ContextCompat would matter more if you wanted to bring along themed drawables.

Jordan Cox
Jordan Cox
8,930 Points

OK thank you :) I did try that first from the google doc you posted, but I must have mispelled or done something stupid :) It seems to be working!

I also don't really understand "optimise your imports"? You mean alt+enter?

Thanks again!

Ctrl + Alt + O

Which optimizes your import statements in one go. In this case though, Alt + Enter also would have worked since it would just fix the one problem.

Jordan Cox
Jordan Cox
8,930 Points

Thanks again! I don't know how to give you some thumbs up or extra credit for being an android genius (like myself ;) ) but let it be known that James Simshaw is the master!