Working with Resources

You've worked with resources a few times in this book already, and it's probably familiar to you at this point to use the R class to access resources from within your application. If you're still a bit rusty on resources and the generated R file, see Chapter 5.

As you work more and more with resources, you find a number of common problems. For one thing, it's pretty easy to overlook the tedious chore of inserting strings into your resources. For another, it can be difficult to size images correctly. The next two sections show you how to deal with each of these problems. And the last section shows you how to do something with resources that's very useful — how to make your apps understood around the globe.

Moving strings into resources

When developing a project, I've been known to take a few shortcuts. Because of this, at times I've forgotten to put strings into resources, and I've had to come back later to do this. Because this is a pretty common error, I've actually done this on purpose with the Screen Brightness Toggle application. Here I walk you through the process of inserting a string into a resource using the built-in tools.

Before I do that, though, let me show you the “long way”:

  1. Create a new string resource.
  2. Copy its name.
  3. Replace the string value in your layout with the resource identifier.

Okay, this may not be a huge pain, but it takes time, possibly 30–45 seconds for the average developer. Now I show you how to cut that number to under 15 ...

Get Android™ Tablet Application Development For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.