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

Matthew Francis
Matthew Francis
6,967 Points

When to use getResource()?

From this post:http://stackoverflow.com/questions/7788390/when-and-why-should-one-use-getresources

It states that you should getResource() for accesing the methods in Resource.class

However, why can you do:

getString(R.id.whateverName)? getString is inside the Resource.class, it dosent make sense.

When you try to get a drawable file, you need to do:

getResource.getDrawable(R.id.whateverPic)

Why dosent it work when you remove getResource()?

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

I'm not on a computer with Android Studio so I can't check it out, but I think this is just because getString() is not ambiguous like getDrawable(). There's an ImageView.getDrawable() and a Resources.getDrawable().

Note: Resources.getDrawable() is now deprecated. See this post: Android getResources().getDrawable() deprecated API 22.