How to use Spinner in android.

In this Article we create how to use Spinner in android. And we are going to display the fruits list. You need to use ArrayAdapter class to store the fruits list.

Android spinner is like the drop down menu with multiple values. It is use to display the multiple options to the user in which user can select only one item.

Android Spinner is like the combox box of Swing.

 

//Simple Spinner Example in android

Step 1: Firstly, open your activity_main.xml file and add following code ( Open res -> layout -> activity_main.xml and add following code:)

// activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#d3ffce"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />
</RelativeLayout>


Step 2: Let’s  open .Java file and and add following code (Open src -> package -> MainActivity.java and add following code: )

package com.example.thakur.spinner;
import android.app.Activity;
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 Activity implements AdapterView.OnItemSelectedListener {
Spinner sp;
String[] fruits = {"select any one...","Apple", "Banana", "Guava", "Strawberry", "Grapes"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,fruits);
ad.setDropDownViewResource(android.R.layout.simple_expandable_list_item_1);
sp.setAdapter(ad);
sp.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id){
Toast.makeText(getApplicationContext(),fruits[position] ,Toast.LENGTH_LONG).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(this,"ANDROID",Toast.LENGTH_LONG).show();
}
}

Step 3: Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.thakur.spinner">
 
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         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>

Step 4 : Our output will be like this :

1

 

2

 

3

I hope you like this Article.

Tags: , ,