String Arrays

Description

You can specify an array of strings as a resource in any file under the /res/values subdirectory.

Example

You can use an XML node called string-array. This node is a child node of resources.

The following code shows an example of an array in a resource file.


<?xml version="1.0" encoding="utf-8"?>
<resources>
      <string name="hello">hello</string>
      <string name="app_name">hello appname</string>
      <string-array name="test_array">
                 <item>one</item>
                 <item>two</item>
                 <item>three</item>
      </string-array>
</resources>

Once you have this string-array resource definition, you can retrieve this array in the Java code as follows.


// w w  w  . j  a v a 2 s  .  com
//Get access to Resources object from an Activity
Resources res = your_Activity.getResources();
String strings[] = res.getStringArray(R.array.test_array);

//Print strings
for (String s: strings)
{
    Log.d("example", s);
}




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS