In Section 4.5.8, you created a String
array resource in the app’s strings.xml
file. Technically, all of your app’s resources in the res/values
folder can be defined in the same file. However, to make it easier to manage different types of resources, separate files are typically used for each. For example, array resources are normally defined in arrays.xml
, colors in colors.xml
, String
s in strings.xml
and numeric values in values.xml
. This app uses three String
array resources that are defined in arrays.xml
:
• regions_list
specifies the names of the world regions with their words separated by underscores—these values are used to load image file names from the appropriate folders and as the selected values for the world regions the user selects in the SettingsFragment
.
• regions_list_for_settings
specifies the names of the world regions with their words separated by spaces—these values are used in the SettingsFragment
to display the region names to the user.
• guesses_list
specifies the String
s 3
, 6
and 9
—these values are used in the SettingsFragment
to display the options for the number of guess Button
s to display.
Figure 5.10 shows the names and element values for these three array resources.
To create the file and configure the array resources, perform the following steps:
1. In the project’s res
folder, right click the values
folder, then select New > Android XML File to display the New Android XML File dialog. Because you right clicked the values
folder, the dialog is preconfigured to add a Values resource file in the values
folder.
2. Specify arrays.xml
in the File field and click Finish to create the file.
3. If the IDE opens the new file in XML view, click the Resources tab at the bottom of the window to view the resource editor.
4. To create a String
array resource, click Add..., select String Array and click OK.
5. In the Name field, enter regions_list
, then save the file.
6. Select the new String
array resource, then use the Add button to add items for each of the values shown for the array in Fig. 5.10.
7. Repeat Steps 4–6 for the regions_list_for_settings
and guesses_list
arrays. When you click Add... to create the additional String Array resources, you’ll need to first select the radio button Create a new element at the top level in Resources.