xml
string array
string resources
android development
xml referencing

Referencing a string in a string array resource with xml

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Android stores user-facing strings in XML resource files (res/values/strings.xml) for localization and maintainability. A string-array groups related strings together, useful for spinners, list views, and dropdown menus. You reference individual string array items programmatically in Java/Kotlin using getResources().getStringArray(), but you cannot directly reference a single item from a string-array in XML layouts. Instead, you either reference the entire array or define individual <string> resources and compose the array from those references.

Defining a String Array

String arrays are defined in res/values/strings.xml (or any XML file in res/values/):

xml
1<!-- res/values/strings.xml -->
2<resources>
3    <string-array name="planets">
4        <item>Mercury</item>
5        <item>Venus</item>
6        <item>Earth</item>
7        <item>Mars</item>
8        <item>Jupiter</item>
9    </string-array>
10</resources>

Referencing the Array in XML Layouts

You can reference the entire array in layout XML using @array/name:

xml
1<!-- res/layout/activity_main.xml -->
2<Spinner
3    android:id="@+id/planet_spinner"
4    android:layout_width="match_parent"
5    android:layout_height="wrap_content"
6    android:entries="@array/planets" />

The android:entries attribute accepts an array resource and automatically populates the spinner with all items.

Accessing Array Items in Code

To access individual elements from a string array, use getResources().getStringArray():

java
1// Java
2String[] planets = getResources().getStringArray(R.array.planets);
3String earth = planets[2];  // "Earth"
4
5// Display in a custom adapter
6ArrayAdapter<String> adapter = new ArrayAdapter<>(
7    this,
8    android.R.layout.simple_spinner_item,
9    planets
10);
11adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
12spinner.setAdapter(adapter);
kotlin
// Kotlin
val planets = resources.getStringArray(R.array.planets)
val earth = planets[2]  // "Earth"

Composing Arrays from String References

You cannot reference a single <item> from a string-array in XML. The workaround is to define individual <string> resources and reference them inside the array:

xml
1<!-- res/values/strings.xml -->
2<resources>
3    <!-- Individual string resources -->
4    <string name="planet_mercury">Mercury</string>
5    <string name="planet_venus">Venus</string>
6    <string name="planet_earth">Earth</string>
7    <string name="planet_mars">Mars</string>
8    <string name="planet_jupiter">Jupiter</string>
9
10    <!-- Array composed from string references -->
11    <string-array name="planets">
12        <item>@string/planet_mercury</item>
13        <item>@string/planet_venus</item>
14        <item>@string/planet_earth</item>
15        <item>@string/planet_mars</item>
16        <item>@string/planet_jupiter</item>
17    </string-array>
18</resources>

Now you can reference individual strings in XML layouts:

xml
1<!-- Reference a single string anywhere in XML -->
2<TextView
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:text="@string/planet_earth" />

And the array still works for spinners and list views:

xml
1<Spinner
2    android:entries="@array/planets"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content" />

Integer and Typed Arrays

Android also supports integer arrays and typed arrays:

xml
1<!-- Integer array -->
2<integer-array name="prime_numbers">
3    <item>2</item>
4    <item>3</item>
5    <item>5</item>
6    <item>7</item>
7    <item>11</item>
8</integer-array>
9
10<!-- Typed array (for mixed resource references like drawables) -->
11<array name="planet_icons">
12    <item>@drawable/ic_mercury</item>
13    <item>@drawable/ic_venus</item>
14    <item>@drawable/ic_earth</item>
15</array>
java
1// Access integer array
2int[] primes = getResources().getIntArray(R.array.prime_numbers);
3
4// Access typed array (for drawables, colors, etc.)
5TypedArray icons = getResources().obtainTypedArray(R.array.planet_icons);
6Drawable mercuryIcon = icons.getDrawable(0);
7icons.recycle();  // Always recycle TypedArray

Localization with String Arrays

String arrays are localized just like regular strings by creating locale-specific resource directories:

 
1res/
2├── values/
3│   └── strings.xml          (default / English)
4├── values-es/
5│   └── strings.xml          (Spanish)
6└── values-fr/
7    └── strings.xml          (French)
xml
1<!-- res/values-es/strings.xml -->
2<resources>
3    <string-array name="planets">
4        <item>Mercurio</item>
5        <item>Venus</item>
6        <item>Tierra</item>
7        <item>Marte</item>
8        <item>Júpiter</item>
9    </string-array>
10</resources>

Android automatically selects the correct array based on the device locale.

Common Pitfalls

  • Trying to reference a single array item in XML: There is no @array/planets[2] syntax. Define individual <string> resources and reference them with @string/name if you need to use a specific item in XML layouts.
  • Forgetting to recycle TypedArray: obtainTypedArray() allocates resources that must be freed with recycle(). Not recycling causes memory leaks, especially in list adapters.
  • Mismatched array lengths across locales: If the default array has 5 items but the Spanish translation has only 4, code that accesses index 4 crashes with ArrayIndexOutOfBoundsException. Keep array lengths consistent across all locales.
  • Using @array/ instead of @string-array/: In XML references like android:entries, use @array/name. In code, use R.array.name. The @string-array syntax is only for the definition tag, not for references.
  • Hardcoding strings instead of using resources: Hardcoded strings in layouts or code cannot be localized. Always define strings in res/values/strings.xml and reference them with @string/ or R.string..

Summary

  • Define string arrays in res/values/strings.xml using <string-array> with <item> elements
  • Reference the entire array in XML layouts with @array/name (e.g., for android:entries on a Spinner)
  • Access individual items in code with getResources().getStringArray(R.array.name)[index]
  • To reference individual items in XML, define separate <string> resources and compose the array from @string/ references
  • Always recycle TypedArray objects and keep array lengths consistent across locales

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.