Populating spinner directly in the layout xml
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An Android Spinner can be populated entirely from XML when the list of choices is static. This is a useful technique for simple forms because it keeps display data in resources, reduces adapter boilerplate, and makes localization easier.
The basic XML approach
There are two pieces involved:
- the
Spinnerview in your layout - a string-array resource that supplies the entries
In the layout file, point the spinner at an array resource with android:entries:
Then define the array in res/values/arrays.xml:
At runtime, Android creates a default ArrayAdapter behind the scenes and uses those values as the spinner options.
Why this is useful
This style works well when:
- the choices are fixed
- the list is part of the app’s resources
- you want translators to manage labels through standard resource files
It is also easier to read. A future maintainer can open the layout and immediately see that the spinner is backed by a resource array instead of hunting through activity code.
Reading the selected value in code
Populating the spinner from XML does not remove the need for code when you want to respond to a selection. You still access the view normally:
If you need to react when the user changes the selection, add a listener:
The important point is that XML can define the items, while code can still control the behavior.
When XML is not enough
This technique is only appropriate for static or mostly static values. If the spinner items come from a server, a database, or user input, you need to build and update an adapter programmatically.
Programmatic setup is also better when:
- items have custom view layouts
- the list changes after the screen loads
- each row needs more than a single string
A spinner backed by XML is simple because the data is simple.
Styling and localization
Because the entries live in resources, localization is straightforward. You can provide translated arrays in language-specific resource directories without changing your activity code.
If you want a custom row appearance, the XML-only shortcut becomes limiting. In that case, create an ArrayAdapter or custom adapter in code and use a layout resource designed for spinner rows.
Common Pitfalls
The most common mistake is expecting XML-defined entries to work for dynamic data. They will not update automatically from network responses or runtime state changes.
Another issue is forgetting that spinner items are user-facing strings. If you later need stable IDs for business logic, a simple string-array may not be expressive enough and a custom model list is better.
Developers also sometimes assume android:entries gives them full adapter customization. It does not; it is a convenience feature for straightforward cases.
Finally, be careful with default selection behavior. A spinner usually starts with the first item selected, so code that assumes "nothing selected yet" can behave unexpectedly.
Summary
- Use
android:entrieswith astring-arrayresource to populate a spinner directly from XML. - This approach is best for static, localizable lists.
- You can still read the selected value and attach listeners in code.
- Switch to a programmatic adapter when the data is dynamic or the row layout is custom.
- Keep resource-based spinners simple and user-facing to get the most benefit from the XML approach.
Related reading
- position fixed doesn't work on iPad and iPhone
- Positioning MKMapView to show multiple annotations at once
- Possible to change where Android Virtual Devices are saved?
- Possible to handle your own http URL schemes in iOS?
- POST Multipart Form Data using Retrofit 2.0 including image
- POST request with a simple string in body with Alamofire
- Posting a runnable to a View that invalidates the View sometimes doesn't work
- Posting NSNotification on the main thread
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.