Working with Resources

You may have worked with resources a few times in this book, and at this point you’re likely familiar with using the R class to access resources from within your application. If you’re rusty on resources and the generated R file, see Chapter 3.

Moving strings into resources

As you become an experienced programmer, you may start to take shortcuts to get your project built and working. Say that initially you forget to move strings into resources, and you have to come back at a later time to do it. You can extract a string into a resource using the built-in tools.

The long way

Here’s one way to extract a string into a resource:

1. Create a new string resource.

2. Copy its name.

3. Replace the string value in your layout with the resource identifier.

This task may not be a huge pain, but it takes time, possibly 30 to 45 seconds for the average developer.

The fast way

You can cut the time to create a string resource to fewer than 15 seconds. If you do this 30 times a day (which is feasible in an 8-hour day), you can save 15 minutes of just copying and pasting. That’s five hours a month doing the copy-and-paste dance!

Follow these steps:

1. In Eclipse, open the main.xml file in the layouts directory.

2. Find the following chunk of code in the file:

<Button

android:id=”@+id/toggleButton”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_gravity=”center_horizontal”

android:text=”Toggle Silent Mode”

/>

3. Select ...

Get Android Application Development For Dummies, 2nd Edition 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.