Android Spinner Tutorial/ Android Dropdown List Tutorial

This tutorial explains, how to create spinner layout, add values in dropdown list and user can select value from the dropdown menu.

Introduction

Don't Miss

The Spinner is a ui-element as similar to dropdown list. A tap on spinner opens a dropdown menu with available values and from that list. We can easily select value from pre-defined set of values.

Create a New Project

Create a new Project in android Studio goto File ⇒ New ⇒ New Projects.

Creating Layout

Don't Miss

We will create layout of spinner and also textview to display selected value of Spinner. Open activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

        <spinner android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:id="@+id/spinner" 
		android:gravity="left"
		android:layout_margintop="50dp" 
		app:layout_constraintleft_toleftof="parent" 
		app:layout_constraintright_torightof="parent"
		app:layout_constrainttop_totopof="parent"/>

        <textview android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:id="@+id/text" 
		android:layout_centerinparent="true" 
		android:textsize="40dp" 
		android:textcolor="#000000" 
		app:layout_constraintbottom_tobottomof="parent" 
		app:layout_constraintleft_toleftof="parent" 
		app:layout_constraintright_torightof="parent" 
		app:layout_constrainttop_totopof="parent"/>

</androidx.constraintlayout.widget.constraintlayout>
Android Spinner

Creating an Array List

We need to define SpineAdapter, to populate list in dropdown. We can provided values to SpinnerAdapter in various ways such as ArrayAdapter if values are stored in array or through CursorAdapter. We will use ArrayAdapter to provide list to SpinnerAdapter. Add the following code to your MainActivity.java file.
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    List list;
    ArrayAdapter SpinnerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);
        list = new ArrayList(); // List of Items
        list.add("All State");
        list.add("New Delhi");
        list.add("Haryana");
        list.add("Bihar");
        list.add("Gujrat");
        list.add("Kerala");

        // Calling the ArrayAdapter with argument context,
        // layout resource and list of items.
        SpinnerAdapter = new ArrayAdapter
                (this, android.R.layout.simple_spinner_item, list){
            //By using this method we will define how
           // the text appears before clicking a spinner
            public View getView(int position, View convertView,
                                ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView) v).setTextColor(Color.parseColor("#E30D81"));
                return v;
            }
            //By using this method we will define
            //how the listview appears after clicking a spinner
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View v = super.getDropDownView(position, convertView,
                        parent);
                v.setBackgroundColor(Color.parseColor("#E30D81"));
                ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                return v;
            }
        };
        SpinnerAdapter.setDropDownViewResource(
             android.R.layout.simple_spinner_dropdown_item);
         // Set Adapter in the spinner
        spinner.setAdapter(SpinnerAdapter);
    }
}

AndroidSpinner

Set Listener

When we will select an item from the dropdown list, we need to add listener for spinner, which call the setOnItemSelectedListener method. The setOnItemSelectedListener listens for selections of list of items.
 spinner.setOnItemSelectedListener(new
           AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parentView,
                       View selectedItemView, int position, long id) {
                // selected item in the list
                String state = parentView.getItemAtPosition(position).toString();
                ((TextView) findViewById(R.id.text)).setText(state);
            }
            @Override
            public void onNothingSelected(AdapterView parentView) {
                // your code here
            }
        });
AndroidSpinner1

Final Code

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    List list;
    ArrayAdapter SpinnerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);
        list = new ArrayList(); // List of Items
        list.add("All State");
        list.add("New Delhi");
        list.add("Haryana");
        list.add("Bihar");
        list.add("Gujrat");
        list.add("Kerala");

        // Calling the ArrayAdapter with argument context,
        //layout resource and list of items.
        SpinnerAdapter = new ArrayAdapter
                (this, android.R.layout.simple_spinner_item, list){
            //By using this method we will define how
           //the text appears before clicking a spinner
            public View getView(int position, View convertView,
                                ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView) v).setTextColor(Color.parseColor("#E30D81"));
                return v;
            }
            //By using this method we will define
            //how the listview appears after clicking a spinner
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View v = super.getDropDownView(position, convertView,
                        parent);
                v.setBackgroundColor(Color.parseColor("#E30D81"));
                ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                return v;
            }
        };
        SpinnerAdapter.setDropDownViewResource(
                  android.R.layout.simple_spinner_dropdown_item);
      // Set Adapter in the spinner
        spinner.setAdapter(SpinnerAdapter);
        spinner.setOnItemSelectedListener(new
                   AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parentView,
                       View selectedItemView, int position, long id) {
                // selected item in the list
                String state = parentView.getItemAtPosition(position).toString();
                ((TextView) findViewById(R.id.text)).setText(state);
            }
            @Override
            public void onNothingSelected(AdapterView parentView) {
                // your code here
            }
        });
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Recent Posts

Basic Geometry Math Formula List

Geometry is a department of mathematics that offers with shape, dimension, relative position of figures, and the properties of space. It emerges independently in variety of early cultures as a practical approach of dealing with lengths, area and volumes.

How to Sign in with Google in Android Tutorial

In this tutorial, we are going to explain how to sign in with Google in Android Application. Many apps want to...

Trigonometry Math Formulas List

Trigonometry is a department of mathematics that offers with triangles. Trigonometry is also called the study of relationships between lengths and angles of triangles. There is a gigantic variety of makes...

Related Articles

Basic Geometry Math Formula List

Geometry is a department of mathematics that offers with shape, dimension, relative position of figures, and the properties of space. It emerges independently in variety of early cultures as a practical approach of dealing with lengths, area and volumes.

How to Sign in with Google in Android Tutorial

In this tutorial, we are going to explain how to sign in with Google in Android Application. Many apps want to...

Trigonometry Math Formulas List

Trigonometry is a department of mathematics that offers with triangles. Trigonometry is also called the study of relationships between lengths and angles of triangles. There is a gigantic variety of makes...