Android Spinner Tutorial - Spinner Style and Example | W3schools

Spinner is an Android User Interface (UI) control which lets a user select an option from the drop-down list.

Also Read: Android RadioButton Control

In the above screenshot of Gmail application, you can easily see a drop-down menu with three options, which are Reply, Reply All, and Forward. The Spinner UI control makes it possible for the user to select any one of the provided options from a drop-down list.
In the below example, we will be explaining a step-to-step guide on how to use this Spinner control in your Android application. In this application, you will be displaying a drop-down menu to the user which will be carrying a list of categories.

Step 1: Let’s start with the creation of an Android application on the Android Studio IDE. Save this application as AndroidSpinnerExample. Make sure you store this application under the com.example.spinner package.

[post_middile_section_ad]

Step 2: You need to update the AndroidSpinnerExampleActivity.java file. You can easily locate this file under the src/com.example.spinner directory. Here, you will be creating a list view for the items to be displayed on the Spinner.

package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener; 

class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener
{
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);      

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

      // Spinner click listener
      spinner.setOnItemSelectedListener(this);      

      // Spinner Drop down elements
      List<String> categories = new ArrayList<String>();

      categories.add(“Automobile”);
      categories.add(“Business Services”);
      categories.add(“Computers”);
      categories.add(“Education”);
      categories.add(“Personal”);
      categories.add(“Travel”);      

      // Creating adapter for spinner
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);      

      // Drop down layout style – list view with radio button
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);      

      // attaching data adapter to spinner
      spinner.setAdapter(dataAdapter);
   }   

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
   {
      // On selecting a spinner item
      String item = parent.getItemAtPosition(position).toString();      

      // Showing selected spinner item
      Toast.makeText(parent.getContext(), “Selected: ” + item, Toast.LENGTH_LONG).show();
   }

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

[post_middile_section_ad]

Step 3: Add the below provided XML code to the main_activity.xml file. Trace the res/layout/ path to locate this file.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
   android:orientation=”vertical”
   android:padding=”10dip”
   android:layout_width=”fill_parent”
   android:layout_height=”wrap_content”>   

   <TextView
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”
      android:layout_marginTop=”10dip”
      android:text=”Category:”
      android:layout_marginBottom=”5dp”/>      

   <Spinner
      android:id=”@+id/spinner”
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”
      android:prompt=”@string/spinner_title”/>      
</LinearLayout>

Step 4: Android Studio will take care of the definitions of the new string constants. This section of the application will automatically occur in the strings.xml file. You can find this file under the res/values/ directory. 

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>AndroidSpinnerExample</string>
</resources> 

[post_middile_section_ad]

Step 5: The already existing content of the AndroidManifest.xml file is shown below.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.spinner” > 
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >      

      <activity       android:name=”com.example.spinner.AndroidSpinnerExampleActivity”
         android:label=”@string/app_name” >     
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>      
      </activity>      
   </application>
</manifest>

Step 6: Now, you need to run the AndroidSpinnerExample application on the Android Studio IDE. Before we proceed any further, we are assuming that you have already created your AVD during the environment setup. Open one of the activity files of your project and Run the application. The successful completion of the process will display the final output on the Emulator window.

Once you click on the Automobile spinner button, it will appear as a drop-down menu displaying various categories on the screen.

[post_middile_section_ad]