String Array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the
name attribute (not the name of the XML file). As such, you can combine string array resources with other simple resources in the one XML file, under one
<resources> element.
file location:res/values/filename .xml
The filename is arbitrary. The <string-array> element's
name will be used as the resource ID.compiled resource datatype:Resource pointer to an array of
String
s.resource reference:In Java: R.array.string_array_name
syntax:<? xml version = "1.0"
encoding = "utf-8" ?>
<
resources >
<
string-array
name = " string_array_name
" >
<
item
> text_string </item>
</string-array>
</resources> elements:<resources>
Required. This must be the root node.
No attributes.
<string-array> Defines an array of strings. Contains one or more
<item> elements.
attributes:
name String . A name for the array. This name will be used as the resource ID to reference the array.<item> A string, which can include styling tags. The value can
be a referenced to another string resource. Must be a child of a
<string-array> element. Beware that you must escape apostrophes and quotation marks. See
Formatting and Styling , below, for information about to properly style and format your strings.
No attributes.
example:XML file saved at res/values/strings.xml :
<? xml version = "1.0"
encoding = "utf-8" ?>
<resources>
<string-array name = "planets_array"
>
<item> Mercury </item>
<item> Venus </item>
<item> Earth </item>
<item> Mars </item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res =
getResources () ;
String [] planets =
res .
getStringArray ( R .
array . planets_array );