A Simple Spinner


Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.



Spinners are available as part of standard UI widgets in Android studio libraries.


A simple code to use spinner as a drop down selector to select the city.

eg

AndroidManifest.xml:
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ourcoaching.simplespinner">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
intent-filter
>
activity>
application>

manifest>


activity_main.xml:

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.ourcoaching.simplespinner.MainActivity">


<Spinner
android:id="@+id/spinner"
android:layout_width="243dp"
android:layout_height="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android.support.constraint.ConstraintLayout
>


UI layout:


Backend Code:

Steps to use a spinner element:

1. Instantiate a spinner object from UI code.

2. Use a class ArrayAdapter to define the elemtens of spinner.

3. Instantiate the arrayadapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,STRING_LIST/ARRAY)

4. Set dropdown feature arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

5. Set arrayadapter to spinner object : spinner.setAdapter(arrayAdapter)

6. Set an on click listner :  spinner.setOnItemSelectedListener(this). This will require you to declare interface to MainActivity.class : AdapterView.OnItemSelectedListener. After adding the interface, you need to define two default methods ie onItemSelected and onNothingSelected.


MainActivity.java:

package com.ourcoaching.simplespinner;

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 implements AdapterView.OnItemSelectedListener{

private String cities[];
private Spinner spinner;
private ArrayAdapter arrayAdapter;

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

spinner=(Spinner)findViewById(R.id.spinner);

cities=new String[5];
cities[0]="Delhi";
cities[1]="Kolkata";
cities[2]="Mumbai";
cities[3]="Chennai";
cities[4]="Jaipur";

arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,cities);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
Toast.makeText(this,cities[i],Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView adapterView) {

}
}



Download Code from GitHub




Related Tutorials