Android Create spinner programmatically from array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a Spinner programmatically is useful when the options depend on runtime data or when the entire view hierarchy is being built in code. The core steps are simple: create the Spinner, supply an adapter backed by your array, choose item layouts, and add the view to a parent container. The part that usually goes wrong is not the spinner itself, but forgetting that adapters, layout params, and selection callbacks all still matter when no XML is involved.
Create the Spinner and Data Source
A spinner needs a Context and a list of values. In Kotlin, a straightforward setup looks like this:
At this point, the spinner exists but has nothing to display. The next step is to connect it to an adapter.
Attach an ArrayAdapter
For simple text entries, ArrayAdapter is the normal choice.
Two layout resources are involved here:
- '
simple_spinner_itemfor the collapsed view' - '
simple_spinner_dropdown_itemfor the drop-down rows'
If you skip setDropDownViewResource, the spinner may still work, but the drop-down appearance can be inconsistent.
Add the Spinner to a Parent Layout
Because the view is created in code, it must also be inserted into an existing container.
A complete example combining the adapter and parent layout looks like this:
With this in place, the spinner behaves just like one declared in XML.
Handle Selection Events
A spinner is usually created because the app needs the selected value. Attach a listener as usual.
Remember that onItemSelected can fire during initialization, not only after a user tap. If that matters, guard the first callback explicitly.
Update the Spinner Later
If the list of items changes at runtime, update the adapter rather than replacing the whole spinner.
That is usually cleaner than creating a new spinner every time the data changes.
When to Use a Custom Item Layout
The built-in layouts are fine for plain text. If you need icons, custom fonts, or more precise spacing, use your own row layout XML and pass it into the adapter. The rest of the programmatic spinner setup stays the same.
A good rule is:
- use the built-in layout for simple text choices
- use a custom row layout only when the UI actually needs it
Common Pitfalls
A common mistake is creating the spinner but never adding it to a parent layout. In that case, the code runs and no widget appears.
Another issue is forgetting to set the drop-down view resource. The spinner may render awkwardly or inconsistently depending on theme and Android version.
Developers also sometimes build the spinner correctly but then expect onItemSelected to fire only after user interaction. The spinner often reports an initial selection automatically.
Finally, if you are building views dynamically, keep all UI work on the main thread. Creating and attaching Android views from background threads will eventually fail.
Summary
- Create the
Spinnerin code, then connect it to anArrayAdapterbuilt from your array. - Set both the item layout and the drop-down layout for predictable rendering.
- Add the spinner to a real parent container and give it layout params.
- Attach a selection listener if the app needs the chosen value.
- Update the adapter, not the whole view, when the item list changes at runtime.

