Skip to main content

How to populate a Spinner with static data in Kotlin Android

How to populate a Spinner with static data in Kotlin Android.

Here is a step-by-step tutorial on how to populate a Spinner with static data in Kotlin for Android:

Step 1: Create a new Android project

Start by creating a new Android project in Android Studio. Choose an appropriate project name, package name, and minimum SDK version.

Step 2: Design the layout

Open the layout file (e.g., activity_main.xml) and add a Spinner element to it. For example, you can use the following code to add a Spinner with an id of "spinner":

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Step 3: Define the data source

In your activity or fragment file, define the static data that you want to populate the Spinner with. You can use an array, list, or any other data structure to hold the items. For example, let's use an array of strings:

val data = arrayOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")

Step 4: Create an ArrayAdapter

Next, create an ArrayAdapter to provide the data to the Spinner. Pass the context, layout resource for the spinner item, and the data source to the ArrayAdapter constructor. For example:

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, data)

Step 5: Customize the Spinner dropdown layout (optional) By default, the ArrayAdapter uses a simple layout for the Spinner dropdown items. If you want to customize the layout, you can create a custom layout XML file and pass it to the ArrayAdapter constructor as the layout resource. For example, create a custom layout file called "spinner_item.xml" with the following code:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000" />

Then, pass the custom layout resource to the ArrayAdapter constructor:

val adapter = ArrayAdapter(this, R.layout.spinner_item, data)

Step 6: Set the adapter on the Spinner

Finally, set the ArrayAdapter as the adapter for the Spinner by calling the setAdapter() method on the Spinner object. For example:

val spinner = findViewById<Spinner>(R.id.spinner)
spinner.adapter = adapter

That's it! You have successfully populated a Spinner with static data in Kotlin for Android. Run your app to see the Spinner with the populated data.

Here's the complete code for your reference:

class MainActivity : AppCompatActivity() {

val data = arrayOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, data)
// OR
// val adapter = ArrayAdapter(this, R.layout.spinner_item, data)

val spinner = findViewById<Spinner>(R.id.spinner)
spinner.adapter = adapter
}
}

Feel free to modify the data source and customize the layout to fit your specific requirements.