Skip to main content

How to display a Spinner in a dropdown menu in Kotlin Android

How to display a Spinner in a dropdown menu in Kotlin Android.

Here is a detailed step-by-step tutorial on how to display a Spinner in a dropdown menu 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 and set the minimum SDK version to at least 21.

Step 2: Define the Spinner in the layout XML file Open the activity_main.xml file and add the following code to define the Spinner in the layout:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:entries="@array/spinner_items"
android:prompt="@string/spinner_prompt" />

In this code, we define a Spinner with an id of "spinner". We set its width and height to wrap content, and add some margin on the top. We also specify the items for the Spinner using the entries attribute, which points to an array resource called "spinner_items". Finally, we set a prompt for the Spinner using the prompt attribute, which points to a string resource called "spinner_prompt".

Step 3: Define the array resource

Open the strings.xml file and add the following code to define the array resource for the Spinner items:

<string-array name="spinner_items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>

In this code, we define a string array resource called "spinner_items" and add three items to it.

Step 4: Create a reference to the Spinner in the MainActivity Open the MainActivity.kt file and add the following code to create a reference to the Spinner:

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

In this code, we use the findViewById method to find the Spinner with the id "spinner" and assign it to the spinner variable.

Step 5: Create an ArrayAdapter for the Spinner

Add the following code to create an ArrayAdapter for the Spinner and set it as the adapter for the Spinner:

val spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_items, android.R.layout.simple_spinner_item)
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = spinnerAdapter

In this code, we create an ArrayAdapter using the createFromResource method, passing the context, the array resource for the Spinner items, and the layout for the Spinner items as parameters. We also set the layout for the dropdown view items using the setDropDownViewResource method. Finally, we set the ArrayAdapter as the adapter for the Spinner.

Step 6: Handle item selection in the Spinner

Add the following code to handle item selection in the Spinner:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
val selectedItem = parent.getItemAtPosition(position) as String
Toast.makeText(this@MainActivity, "Selected item: $selectedItem", Toast.LENGTH_SHORT).show()
}

override fun onNothingSelected(parent: AdapterView<*>) {
// Do nothing
}
}

In this code, we set an OnItemSelectedListener for the Spinner using the onItemSelectedListener property. We override the onItemSelected method to get the selected item from the Spinner and display a Toast message with the selected item.

Step 7: Run the app

Run the app on an emulator or a physical device to see the Spinner in action. You should be able to see the Spinner with the dropdown menu containing the items defined in the array resource. When you select an item, a Toast message should be displayed with the selected item.

That's it! You have successfully displayed a Spinner in a dropdown menu in Kotlin for Android.