In this tutorial, we’ll be discussing and implementing Spinners in our Android Application using Kotlin. Android Spinner is used to create a drop-down list on the screen.

What will you learn?

  • Creating Spinners through XML and Programmatically
  • Setting a prompt on the Spinner.
  • Creating a custom layout for the Spinner.
  • Handling Click Listeners and Display a Toast message.
  • Preventing the Click Listener from being fired automatically for the first time.

What is Android Spinner?

Spinners are like a drop-down menu that contains a list of items to select from. Once a value is selected the Spinner returns to its default state with that selected value.

After Android 3.0, it’s not possible to display a prompt in a Spinner as the default state in the Spinner. Instead, the first item is displayed.

Data inside a spinner is loaded with an Adapter.

Take the following scenario:

Imagine you need to charge your phone. For that, you must connect your phone charger to the electricity board using a pin (adapter). Then the adapter provides your phone with electricity. In Android, the Spinner is like your phone which is loaded with data using an Adapter. The adapter sets the data as well as the layout for the items to be loaded in the Spinner.

Spinner Callback Events

AdapterView.onItemSelectedListener interface is used to trigger the Spinner click event callbacks.

It consists of two methods:

  • onItemSelected
  • onNothingSelected

In the following section, we’ll create a new Android Studio Project and implement Spinners in our Application. We’ll customize the layouts and learn how to handle different scenarios.

Android Spinner Kotlin Project


1. XML Layout Code

The code for the activity_main.xml layout file is given below.

It hosts a single Spinner at the moment android:spinnerMode can be either dialog or dropdown.

To show prompts, you should use dialog as the spinnerMode value.

2. Spinner XML Code

The code for spinner_right_aligned.xml is given below.

3. MainActivity Kotlin Code

The code for the MainActivity.kt class is given below.

Important Points:

  • Thanks to Kotlin Android extensions, the XML Spinner widget is automatically available in our Kotlin Activity class.
  • We’ve created an arrayOf strings that consist of programming languages. These are filled in the adapter using the ArrayAdapter.
  • The setDropDownViewResource is used to set the layout for the selected state and the spinner list rows.
  • The android.R.layout.simple_spinner_item is used to set the default android SDK layout. By default, the TextView is left aligned in this type of layout.

We’ve created a second Spinner programmatically that loads the layouts from the spinner_right_aligned.xml file.

The setSelection(0, false) is used to prevent the Spinner’s OnItemSelected methods from firing when the Activity is created.

How does it work?
The setSelection() method tells the Activity that the first spinner item was already selected. We must place this statement before onItemSelectedListener = this.

The setPopupBackgroundResource is used to set the background color on the dropdown list.

Inside the onItemSelected function, we use the when statement to trigger a Toast for the respective Spinner item.

Thanks to Kotlin and functions with default values, we’ve reduced the verbose call to the Toast.

4. Spinner Kotlin App Output

The following is the output when the above application was run on an emulator.
android spinner kotlin output

You can download the source code of the above project from the link below.

By admin

Leave a Reply

%d bloggers like this: