Adding New Project Resources

Screens with form fields seem to rely on more resources than most other screen types. You need to add a number of new resources to support the settings screen. In addition to the string, dimension, and color resources, you also need to add a new type of resource: a string array.

Adding New String Resources

The settings screen relies on numerous new string resources. You add the following text resources to the strings.xml resource file:

► Text for each setting's TextView label (for example, "Nickname:")

► Text for each Button control (for example, "Set Password")

► Text to display in a TextView control when the password is set or not set

► Text to display in a TextView control when the Date of Birth field is not set

► Text to display in a TextView control when the two Password fields match or don't match

► Text for each Gender option in the Spinner control (for example, "Male") After you have added these strings, save the strings.xml resource file.

Adding New String Array Resources

Spinner controls, like ListView controls, use data adapters. You have your gender string resources defined, but you still need to group them into some sort of dataset.

The simplest dataset is an array (and the corresponding ArrayAdapter control). To group the gender string resources ("Male", "Female", "Prefer Not To Say") together into an array, you create a new resource type called a String array.

To create a String array resource, you must add a new resource file called /res/values/arrays.xml. Within this file, you create a new string-array element called genders. Within this string-array element, you add three Item elements, one for each string resource.

Assume that you added to the strings.xml resource file the following three gender resource strings defined earlier:

<string name="gender_male">Male</string> <string name="gender_female">Female</string> <string name="gender_neutral">Prefer Not To Say</string>

Within the arrays.xml resource file, you set each item in the genders string array to the appropriate string resource. For example, the first Item element in the array (with an index of 0) would have the value @string/gender_neutral. The resulting arrays.xml resource file follows:

<?xml version="i.0" encoding="utf-8"?> <resources>

<string-array name="genders">

<item>@string/gender_neutral</item> <item>@string/gender_male</item> <item>@string/gender_female</item> </string-array> </resources>

Save the arrays.xml resource file. Now when you want to load the genders string array resource into memory, you can access it programmatically by using the R.array.genders resource identifier.

0 0

Post a comment