Simple Dropdown (Spinner)

Now let’s see about Dropdown (Known as Spinner) in android.

In this project, we are going to show a list of android versions in a Spinner.

for that create a new project named Simple Dropdown(you can change the name as your wish :p ).

Now enter the following data in your activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.a4akhilsudha.simpledropdown.MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.112" />

</android.support.constraint.ConstraintLayout>

Now create a string array in your string.xml file. In here we are creating a list of android versions.

<resources>
    <string name="app_name">Simple Dropdown</string>
    <string-array name="android_versions">
        <item>Apple pie</item>
        <item>Banana bread</item>
        <item>Cupcake</item>
        <item>Donut</item>
        <item>Eclair</item>
        <item>Froyo</item>
        <item>Gingerbread</item>
        <item>Honeycomb</item>
        <item>Ice cream sandwich</item>
        <item>Jellybean</item>
        <item>Kitkat</item>
        <item>Lollipop</item>
        <item>Marshmallow</item>
        <item>Nougat</item>
        <item>Oreo</item>
    </string-array>
</resources>

Now open your MainActivity.kt file and enter the following data.

package com.a4akhilsudha.simpledropdown

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast

class MainActivity : AppCompatActivity() {

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

        /*Find the id of spinner*/
        val spinner = findViewById(R.id.spinner) as Spinner

        /*set an adapter with strings array*/
        spinner.adapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, resources.getStringArray(R.array.android_versions))

        /*set click listener*/
        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

                Toast.makeText(this@MainActivity, "You have Selected " + spinner.selectedItem.toString(), Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {

                /*Do something if nothing selected*/


            }
        }
    }
}

Now run (Shift + f10) the program on a real device or in a  virtual device.

Congrats …. 🙂 You just created an android app which displays a drop down menu having some android versions.

Output :

1 thought on “Simple Dropdown (Spinner)

Leave a Reply to Julie Cancel reply

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