---
title: "Spinner in Android"  
description: "Spinner is like a drop-down menu that provides multiple options in the list and selects anyone. I have created a spinner which is used to bind with Ar"  
author: "Nitish Kesarwani"  
published: 2017-12-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12742/spinner-in-android  
category: "android apps"  
tags: ["android", "radiobutton", "dropdown"]  
reading_time: 4 minutes  

---

# Spinner in Android

##### Description

Spinner is like a drop-down menu that provides multiple options in the list and selects anyone. I have created a spinner which is used to bind with ArrayAdapter class. ArrayAdapter class is used to store [string array](https://www.mindstick.com/forum/12803/check-for-any-item-in-string-array) and display into Spinner. It provides scrolling view through which the user can get the desired value. It is a subclass of AsbSpinner. It is associated with AdapterView so, we are using an Adapter class.

[Components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-components) which is introduced in the article is Spinner, TextView, onSelectedItem method. This method is called when action is performed by selecting the value from the menu.

##### Attributes

** android:dropDownHorizontalOffset:** The amount of pixel by which the dropdown adjust itself Horizontally

** android:dropDownVerticalOffset:** The amount of pixel by which the dropdown adjust itself Vertically

** android:dropDownWidth:** To set the width of dropdown when SpinnerMode=”dropdown”

** android: gravity:** This [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin) is used to set the position of the [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) and content

** android: prompt:** The prompt appears when spinner DialogBox is display

** android:popupBackground:** When SpinnerMode=”drop-down” then the [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) drawable is used for drawable

** android: SpinnnerMode:** It provides [display mode](https://www.mindstick.com/interview/2206/what-is-the-use-of-display-modes) for spinner options

** android: dropdownSelector:** This [attribute](https://www.mindstick.com/blog/61/ado-dot-net-object-model-and-attributes) is used when SpinnerMode=”drop-down” is displayed

##### Methods

** onClick():** This method is called when the button in d is clicked

** performClick():** It is performed action when [OnClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach) event is set

** onTouchEvent():** This method is applied to handle touch event on screen

** onSaveInstanceState():** It allows to generate view representation of the internal state which later can be used as a new instance

** onResolvePointerIcon():** It returns pointer icon for motion event ,if it is specified otherwise returns nothing

** getBaseline():** It returns the distance(offset) from widget text baseline from the top boundary of widget

** OnItemSelected():** it returns the selected item from spinner [dropdown list](https://www.mindstick.com/forum/760/dropdown-list-open-hide-behind-another-dropdown-list-on-ie-7)

Android_Mainfest.xml\

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.dropdown">
    <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>
```

###### Main_Activity.xml

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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.mapsample.msclient009.dropdown.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Item"
        android:textColor="#DDA1F1"
        android:textSize="30dp"
        android:gravity="center"
        android:background="#F1f1f1"/>
    <Spinner
        android:id="@+id/DropDown"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_weight="1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:background="#99AA99"
        />
</LinearLayout>
```

######

###### Menuitem_activity.xml

```
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="china">Java</item>
   <item android:title="indian">Android</item>
    <item android:title="south">C++</item>
    <item android:title="punjab">Hibernate</item>
    <item android:title="p">Spring</item>
    <item android:title="s">Struts2</item>
</menu>
```

###### Main_Activity.java

```
package com.mapsample.msclient009.dropdown;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
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 {
    String Language[] = {"Java", "Android", "C++", "Python","Hibernate","Spring","Struts2"};
    Spinner s;
     int count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        s = (Spinner) findViewById ( R.id.DropDown );
        s.setOnItemSelectedListener ( this );
        ArrayAdapter arrayAdapter = new ArrayAdapter ( this, R.layout.support_simple_spinner_dropdown_item, Language );
        arrayAdapter.setDropDownViewResource ( android.R.layout.simple_spinner_dropdown_item );
        s.setAdapter (arrayAdapter);
    }
    public void onItemSelected(AdapterView<?> arg1, View sh,int p,long id)
    { if(arg1!=null) {
        count++;
        Toast.makeText ( getApplicationContext ( ), Language[p] + " List item :" + count, Toast.LENGTH_LONG ).show ( );
    }
    }
    public  void onNothingSelected(AdapterView<?> arg1)
    {Toast.makeText ( getApplicationContext (),"Nothing is selected",Toast.LENGTH_LONG ).show ();
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {getMenuInflater ().inflate ( R.menu.menuitem_activity,menu );
        return  true;
    }
}
```

##### Launch Your APP

![Spinner in Android](https://www.mindstick.com/mindstickarticle/fbb050e5-11a6-4c60-9499-a71499a1d484/images/e4877243-6e5c-4ffd-acb5-3459fcaaa4ac.png) ![Spinner in Android](https://www.mindstick.com/mindstickarticle/fbb050e5-11a6-4c60-9499-a71499a1d484/images/b0d60a63-84e8-43ac-bab7-8d066c255a9a.png)

![Spinner in Android](https://www.mindstick.com/mindstickarticle/fbb050e5-11a6-4c60-9499-a71499a1d484/images/021751a0-a21c-4f93-ac91-baf5c8493789.png)

---

Original Source: https://www.mindstick.com/articles/12742/spinner-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
