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.
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/):
Referencing the Array in XML Layouts
You can reference the entire array in layout XML using @array/name:
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():
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:
Now you can reference individual strings in XML layouts:
And the array still works for spinners and list views:
Integer and Typed Arrays
Android also supports integer arrays and typed arrays:
Localization with String Arrays
String arrays are localized just like regular strings by creating locale-specific resource directories:
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/nameif you need to use a specific item in XML layouts. - Forgetting to recycle
TypedArray:obtainTypedArray()allocates resources that must be freed withrecycle(). 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 likeandroid:entries, use@array/name. In code, useR.array.name. The@string-arraysyntax 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.xmland reference them with@string/orR.string..
Summary
- Define string arrays in
res/values/strings.xmlusing<string-array>with<item>elements - Reference the entire array in XML layouts with
@array/name(e.g., forandroid:entrieson 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
TypedArrayobjects and keep array lengths consistent across locales
Related reading
- Relationship between BFS and topological sort
- Relaxation of an edge in Dijkstra's algorithm
- Removal of negative numbers from an array in Java
- Remove a field from all elements in array in mongodb
- Refresh certain row of UITableView based on Int in Swift
- Refresh devices in team provisioning profile managed by Xcode 7?
- Remove all occurrences of a value from a list?
- Remove all the elements that occur in one list from another

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 courseTrack 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.