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

Marko Jereb
Marko Jereb
1,771 Points

Deprecated getResources().getDrawable(in id)

As of API 22 get.Drawable(int id) is deprecated in Android studio, so my question is if it is ok to use getResources().getDrawable(page.getImageId(), null) or should I use deprecated getDrawable(page.getImageId()) instead?

3 Answers

Change to ContextCompat.getDrawable(context, R.drawable.mydrawable)

Marko Jereb
Marko Jereb
1,771 Points

Thank you for the Answer, so my code looks something like:

mContext = getApplicationContext(); Drawable drawable = ContextCompat.getDrawable(mContext, page.getImageId());

and it works ok.

Alex Vilch
Alex Vilch
10,425 Points

Cant you just use

<p>mImageView.setImageResource(page.getImageId());</p>

I was wondering the same. I tried mImageView.setImageResource(page.getImageId()); and the image changed as expected.

What does getting a drawable object do instead?I was wondering the same. I tried mImageView.setImageResource(page.getImageId()); and the image changed as expected.

What does getting a drawable object do instead?

After looking at the [documentation for setImageResource](http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int) , it might be because of the part about "which can cause a latency hiccup. If that's a concern, consider using setImageDrawable".

I'm guessing that's the major reason why, since setImageResource has been around since API level 1 and does appear to be more clear in general.

It's just a guess though. :)

public void setImageResource (int resId)

Added in API level 1
Sets a drawable as the content of this ImageView.

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.

Related XML Attributes
android:src
Parameters
resId   the resource identifier of the drawable
Kalak Namak
Kalak Namak
1,840 Points

I got it working like this:

        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.page1);
        mImageView.setImageDrawable(drawable);

is this working same as this ? Drawable drawable = getResources().getDrawable(page.getImageId());

//I have same problem like he does... getDrawable is deprecated