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

Lucas Santos
Lucas Santos
19,315 Points

Can I change the ImageView like this?

I chose to change my ImageView differently, is this wrong?

weatherIcon.setImageResource(mCurrentWeather.getIconId());

In the video we would be doing something like this (getResource is deprecated)

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

I tried both ways and they both work, only thing i'm curious about is why Ben chose to go the more complicated route instead of doing it in one simple line?

Is the first method wrong or something?

1 Answer

Darel Bitsy
Darel Bitsy
6,910 Points

Hi, it's more a question of performance and from the android documentation , it's recomended to use setImageDrawable()

setImageResource() : This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Source : https://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29