Spinner In Android | Android Spinner Example With Kotlin

Spinner in Android is a quick way to select a single value from a given set of values. The spinner shows the currently selected value in its default state. When a user clicks a spinner it displays a dropdown list of items from where a user can select a different value. A spinner provides a quick and easy way to select one value from the given list of values. 


Spinner In Android
Spinner In Android/Android Spinner Example


Let’s understand Spinner in Android with a step by step Android Spinner Example:


1- Make a new Android Studio project and name it “AndroidSpinner” and select KOTLIN as the default language for your project.

2- Go to activity_main.xml and first change your default layout from Constraint Layout to Relative Layout.

3- Then add a spinner in your activity_main.xml. layout_width should be “wrap_content” and layout_height should be “wrap_content”. Add marginTop of 100dp and and set layout_centerHorizontal=”true”. The id of our spinner is “spinner”.



4- After adding the spinner this is our final activity_main.xml





5- So, after completing activity_main.xml now we have to add some values in our strings.xml file to display in the spinner. For doing this go to res –> values –> strings.xml. Here we create a String Array to display the values in our spinner.



6- The complete strings.xml file will be like this.




Android Spinner



7- So now in our MainActivity.kt we will populate the spinner and set a Toast message when a spinner item is clicked. We will make use of a Toast variable.



8- After that, we define our Spinner View using findViewById and make use of our String array from our Strings.xml file. Then we populate our array items into the Spinner using an ArrayAdapter.



9- To add a click listener on a single item in the spinner we will make use of AdapterView.OnItemSelectedListener interface¬†because this interface has two callback methods. You must implement these methods otherwise you’ll face an error. When you implement AdapterView.OnItemSelectedListener interface you will see a red line error under your MainActivity class. Press ALT+ENTER and implement both methods to avoid errors.




10- We set a Toast message inside our onItemSelected function.



11- Finally you have to make use of setOnItemSelectedListener() on Spinner inside onCreate() function. Hence will specify the implementation of the interface.



12- So here we complete our Spinner example. Here is the final MainActivity.kt class:



So, I hope now you understand Android Spinner after this Android Studio Spinner example. But if you face any difficulty regarding Spinner Android then you can ask in the comments section below.




Spinner In Android
Android Studio Spinner Example


Spinner In Android
Spinner in Kotlin/Kotlin Spinner


Android Studio Spinner/Spinner Android Studio Example




More Android Posts


Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!