Android
Spinner
XML Layout
UI Development
Android Studio

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.

Browse interview questions

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 Spinner view 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:

xml
1<Spinner
2    android:id="@+id/countrySpinner"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:entries="@array/country_options" />

Then define the array in res/values/arrays.xml:

xml
1<resources>
2    <string-array name="country_options">
3        <item>Canada</item>
4        <item>Germany</item>
5        <item>Japan</item>
6    </string-array>
7</resources>

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:

kotlin
1import android.os.Bundle
2import android.widget.Spinner
3import androidx.appcompat.app.AppCompatActivity
4
5class MainActivity : AppCompatActivity() {
6    override fun onCreate(savedInstanceState: Bundle?) {
7        super.onCreate(savedInstanceState)
8        setContentView(R.layout.activity_main)
9
10        val spinner = findViewById<Spinner>(R.id.countrySpinner)
11        val selected = spinner.selectedItem.toString()
12        println(selected)
13    }
14}

If you need to react when the user changes the selection, add a listener:

kotlin
1import android.view.View
2import android.widget.AdapterView
3import android.widget.Spinner
4
5spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
6    override fun onItemSelected(
7        parent: AdapterView<*>?,
8        view: View?,
9        position: Int,
10        id: Long
11    ) {
12        val value = parent?.getItemAtPosition(position).toString()
13        println("Selected: $value")
14    }
15
16    override fun onNothingSelected(parent: AdapterView<*>?) = Unit
17}

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:entries with a string-array resource 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.