Android Spinner

  • In android, drop down menu is called as spinner.
  • When we want to select one option from the given list then we can use the spinner.
  • In the web programming, <select> tag is used to define the dropdown menu but in android, we can create spinner by using java code or by using the xml <menu> tag.
  • Using spinner, we can define one option or choice from the various items.
  • By default, first element of spinner is shown.
  • When we create the spinner using the xml layout file then we have to define the list of items in the string resource file.
  • For adding the items into the spinner, we have to use the array adapter.
  • If we want to perform some action when we select the list item then we have to implement the onItemSelectedItemListener interface.

Defining Android Spinner

we can define the spinner in the xml layout file. Here we use the tag. There are some elements or attributes as follows:

  • Android:id => it is used to define the id of the spinner.
  • Android:layout_width => it is used to define the width of the spinner.
  • Android:layout_height => it is used to define the height of the spinner.
  • The basic structure of the spinner is as follows:
  • <Spinner
    android:id="@+id/Spinnerid"
    android:layout_width="width-of-spinner"
    android:layout_height="height-of-spinner" />

Creating spinner item list:

  • We can create the spinner item list in the string resource file.
  • For this, we have to create the string array.
  • Inside the tag, we have to define the as the sub element.
  • Now we have to use the name attribute of the to define the name of the array.
  • Then inside the element, we have to use the tag to define the item list for the spinner.
  • The basic syntax is as follows:
  • <resources>
    <string-array name="namOfstringarray">
    <item>Item 1</item>
    <item>Item 2</item>
    <item>Item 3</item>
    </string-array>
    </resources>
  • After creating the list for spinner, now we have to connect them by using the array adaptor.
  • For this, first of all we have to create the object of Spinner class then we have to pass the xml reference of the spinner, pass to the spinner object in the java file.
  • Now we have to create the object of ArrayAdaptor class as follows:
  • ArrayAdapter yrAdapter = new ArrayAdapter();
  • When we use the layout file to create spinner then we cannot attach it to the spinner directly. To achieve this, we have to call the createObjectResource() method using the object of array adaptor.
  • Inside this method, we have to pass the current context, string array, and the item’s layout.
  • The syntax is as follows:
  • yrAdapter.createFromResource(this,R.array.yrstringarray,android.R.layout.simple_spinner_item);
  • Now we have to set the layout which we want to use when item will display.
  • For this, we have to call the setDropDownViewResource() method which takes one parameter as a layout which we want to use.
  • The syntax is as follows:
  • yrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  • Now we have to apply the spinner on the adaptor as follows:
  • yrspinner.setAdapter(yrAdapter);
  • If we want to perform any action then we can.

Here is the sample spinner application in which we have used the java code to set the items in the spinner. The xml layout file is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner Example" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="37dp"
android:layout_marginTop="84dp" />

</RelativeLayout>

Here we have used the relative layout and used a text view that will show the message such as “Spinner Example” and used a tag to define the spinner and inside this tag we have used the various attributes to set the width, height, alignment of the spinner. The java code is as follows:

package com.example.spinnerdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
Spinner sp;
String[] counting = new String[] {"One","Two","Three","Four","Five","Six"};

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

sp = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter adp = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,counting);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adp);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file first we have to include the packages which are as follows:

android-spinner

Here we have used android.widget.ArrayAdapter package for setting the items into the spinner and we have used the android.widget.Spinner package to use the spinner into the java file because we have used the spinner in the xml layout file.

Now first of all we have to declare some variables as follows:

android-spinner

Here we have declared variable sp of Spinner type and we have created a string array named counting that is initialized with the string type of values. After that we have to register the spinner in the java file and assign in to the sp variable of Spinner type as follows:

android-spinner

After that we have created the object of the ArrayAdapter type and we have passed the current context inside this and we have passed the layout for the spinner that is already defined in the android system. Here just we have to use this default layout of spinner, after that we have to pass the string array.

After that we have to call the setDropDownResourse() method that takes the same resource layout where defined in the array adapter, and in the last we have to call the setAdapter() method and it will take the object of the ArrayAdapter.

When application will launch then it will look like as follows:

android-spinner

In this spinner we can select any option from the list as follows:

android-spinner

Selected item will be displayed only in the spinner as follows:

android-spinner