• Have Any Question?
  • contact@eitworld.com

Write an application which demonstrates the spinner and display the selected item

Here we have developed an application in which we have used a spinner and when we select any item from spinner then item will be displayed on the screen. The xml file is as follows:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
tools:context=”.MainActivity” ><TextView
android:text=”Spinner Example” />

android:layout_marginTop=”84dp” />


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.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

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

protected void onCreate(Bundle savedInstanceState) {

sp = (Spinner) findViewById(R.id.spinner1);
message = (TextView) findViewById(R.id.textView1);

ArrayAdapter adp = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,counting);

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> ?>arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String item = sp.getItemAtPosition(arg2).toString();
message.setText(“selected item : “+item);

public void onNothingSelected(AdapterView<?> ?>arg0) {
// TODO Auto-generated method stub


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:


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:

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 and used message variable of type TextView. After that we have to register the spinner in the java file and assign in to the sp variable of Spinner type as follows:

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.
To display which item is selected we have used setOnItemSelected() method, inside this method, we have to add the unimplemented method otherwise we will get error, here when we select on particular item then onItemSelected() method is called which takes four parameters, 1st parameter used for adapter view, 2nd parameter is used for view, 3rd argument is used for position and 4th parameter is used for id of the item. After that we have used getItemAtPostion() and pass the position as parameter and we have converted it in to string and assign it in to the string type variable. After that we have set that variable to display the message to the text view using the setText() method.
The output is as follows:


Real Time Web Analytics