In this tutorial we will learn about how to create spinner in android, and show a toast message of spinner value when user select it and android spinner example .

Android Spinner provides the facility to display a drop down menu in android application. It is used to show the multiple options or items, so that user can select one. The items in the Spinner come from the Adapter associated with the spinner . Spinner is a widget of android.widget.Spinner class .

Spinners provide a nice way to select one value from a set of values. In the default state, a spinner shows its currently selected value . On clicking the spinner it shows a drop down menu in android with all available values, from which the user can select a new one.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

Read Also : Android Beginner’s Guide

2. Create a Spinner Widget to your main layout file :

Here we create a spinner widget to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<Spinner
        android:id="@+id/spnr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="15dp"
        android:spinnerMode="dropdown" />

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textSize="18sp"
        android:text="Spinner :"/>

    <Spinner
        android:id="@+id/spnr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="15dp"
        android:spinnerMode="dropdown" />

</LinearLayout>

3. Implement setOnItemSelectedListener on spinner :

Now implement setOnItemSelectedListener of Spinner to perform an action or event on menu items, like toast message in MainActivity.java file or just add the following code .

spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(parent.getSelectedItem() == "Select a course"){

                }else{
                    Toast.makeText(MainActivity.this, "Selected item is "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

MainActivity.java :

package net.technxt.spinnertest;


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;

public class MainActivity extends AppCompatActivity {

    Spinner spnr;
    String[] course ={"Select a course","Android","Php","Mysql","Java","Angular","Python"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spnr = (Spinner)findViewById(R.id.spnr);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,course);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spnr.setAdapter(adapter);

        spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(parent.getSelectedItem() == "Select a course"){

                }else{
                    Toast.makeText(MainActivity.this, "Selected item is "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
}

Now run the test app to test spinner in android app through android device or any Emulator to add drop down menu in android .

%d bloggers like this: