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 seconds. If you do this 30 times a day (which is feasible in an 8-hour day), you can save 15 minutes of copying and pasting. That's a savings of five hours a month! Follow these steps:

  1. If Eclipse is not open, open it now and open the main.xml file in the layouts directory.
  2. Find the following chunk of code in the file:
    <ToggleButton
               android:id=“@+id/toggleButton”
               android:layout_width=“wrap_content”
               android:layout_height=“wrap_content”
               android:layout_gravity=“center_horizontal”
               android:textOn=“Dimmer On”
               android:textOff=“Dimmer Off”
    />
  3. Select the boldface line “Dimmer On”.
  4. Press Shift+Alt+A.

    This step opens a menu with three options.

  5. Choose the Extract Android String option.

    Doing this opens the Extract Android String dialog box, as shown in Figure 9-1, which allows you to set various options for the resource.

    Figure 9-1: The Extract Android String dialog box.

    image

I'm not going to use any of those features for this, so leave the defaults as they are and click the OK button.

You can now see that the layout file has been modified. The text “Dimmer On” has been replaced with “@string/dimmer_on”. If you open the strings.xml file in the res/values folder, you can see a new string resource with that name, and the value of “Dimmer On”.

Now, that's pretty cool! You can see that doing this 20–30 times a day can add up and save you a lot of time.

Working with images

Dealing with images can be one of the most difficult parts about resources. Images that look great on a medium-density device may look like garbage on a high-density device. This is where the multiple-density folders come into play. These density-specific drawable folders are explained in Chapter 8.

To get around the issue of pixilation and compression/expansion (when going from higher- to lower-density devices and vice versa), design your graphics at a very high resolution, such as 300 dpi in large-size format. Downsizing a high-density image does not distort the quality (other than losing the fine edges and detail), but upscaling does — because it creates pixilation and distortion. Starting with a large file reduces the chances that you'll ever have to upscale, which means that your app graphics will always look crisp.

For example, if you're building your launcher icon, build it at 250px height and 250px width. Although the hdpi folder might need only a 72px-height-x-72px-width image (which is the largest used right now), new Android devices are coming out all the time. In two to three months Google TV may come out, and on a big TV screen, your 72x72 images will look pretty awful.

I know that working with large image files in image-editing programs can be difficult if you don't have a decent-performing computer, but you have to trust me: A large raw image file that is high density is much easier to mold and shape later into the correct densities that you'll need.

Also, if you're creating your graphics in an image-editing tool that supports layers, I highly advise you to place each item in your graphic on a different layer. The reasons for this are many, but here are the key factors:

  • Changes: At some time, you will need to change something in your graphic — maybe the background, maybe the font, maybe the logo. If you have all these items in different layers, you can do that without affecting the rest of the graphic.
  • Localization: You will often encounter graphics with stylized text in the graphic itself. If your application is being translated into Japanese, and your graphics contain stylized English text, you'll want to create a Japanese version of those graphics and place them in a Japanese drawable region folder such as res/drawable-ja. The Android platform recognizes which region it is in (in this case, Japan). If the region's resource folders (res/drawable-ja, res/values-ja, and so on) are available, Android uses those in the application.

Making your apps global with resources

In 2010, the Android platform surpassed Apple's iPhone in U.S. market share, trailing only Research In Motion's BlackBerry, according to ZDNet. Now carriers around the world are developing Android-based smartphones, which means one simple thing: more potential users for your apps.

So what does this mean to you as a developer? It means that Android is a huge market with tons of opportunity waiting to be tapped. This opportunity is very exciting, but to take the greatest advantage of it, you need to understand resources and how they affect the usability of your apps. For example, if your app was written for an English audience (using resources or not), a user in the United States would be able to use it. However, if you hardcoded all of your English string values into your views and activities and then decided to release a Chinese version, you would have to rewrite your application. When you use resources, translations such as this are easy.

Resources allow you to extract human-readable strings, images, and viewable layouts into resources that you can reference. Various resource folders can be created to handle various-size screens, different languages (think strings and drawables), and layout options, such as landscape or portrait. Landscape and portrait views come into play when a user rotates the device 90 degrees in either direction.

If you want your apps to be viewable on as many Android devices as possible around the world, you want to use resources at all times. As an example, I advise that you always put all your strings into the strings.xml file because someday, someone from another country will want your application in another language. To get your application into another language, you simply need to have a translator translate the text in your strings.xml file into the new language, and then you can create various values folders to hold the appropriate region's values. For example, if the user is using a phone set to the Chinese character set, Android knows to look for a values folder in your app called values-cn, which is where Chinese values are stored — including the Chinese version of the strings.xml file. If Android cannot find such a folder, the platform defaults to the default values folder, which contains the English version of the strings.xml file. (For more on strings, see the section “Moving strings into resources,” earlier in this chapter.)

When it comes down to it, having a translator update your strings and adding the new strings.xml file to a new values folder are very simple things to do (compared with recoding your entire application). Take this process and expand it to other languages and devices and eventually Google TV … and you can see where I'm going. You're no longer looking at mobile users as your target audience. You're looking at Android users, and with the options coming out — this could be billions of users. Using resources correctly can make your expansion into foreign markets that much easier.

image Designing your application for various regions is a big topic. You can find more in-depth information in the Localization article of the SDK documentation here: http://d.android.com/guide/topics/resources/localization.html.

image Although designing your application to be ready for various regions sounds compelling, it also helps to know that the Android Market allows you to specify a targeted region for your app. You're not forced into releasing your application to all regions. Therefore, if you have written an application for the Berlin bus route system in Germany, it probably doesn't make sense to have a Chinese version, unless you want to cater to Chinese tourists as well as German residents. I cover the Android Market in depth in Chapter 10.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.153