Spinner Android

Android supports various widgets such as Android Button, Android Toast, Custom Toast, Android ToggleButton, Android CheckBox, Android AlertDialog, Spinner, AutoCompleteTextView, RatingBar, DatePicker, TimePicker, and ProgressBar. The Spinner is one of these unique and important Android Widgets. Much like a combo box of AWT or Swing, the Android Spinner is used to display multiple options to the user. From the multiple available options, only one item can be selected at a time by the user. Being the subclass of the AsbSpinner class, it can also be simply understood as a drop-down menu with multiple values. The end-user can select only one value from the available values of the drop-down menu. Being associated with AdapterView, the Android Spinner requires one of the adapter classes to be used with it.

Android Spinner Example:

In the above example, we are demonstrating the spinner in android. Here, we are displaying a list by using an ArrayAdapter class to store the list.

activity_main.xml: (File: activity_main.xml)

In the activity_main.xml file, we will drag the Spinner from the pallet.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="MainActivity">
 
   <Spinner
       android:id="@+id/spinner"
       android:layout_width="149dp"
       android:layout_height="40dp"
       android:layout_marginBottom="8dp"
       android:layout_marginEnd="8dp"
       android:layout_marginStart="8dp"
       android:layout_marginTop="8dp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintHorizontal_bias="0.502"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintVertical_bias="0.498" />
 
</android.support.constraint.ConstraintLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we are adding the code to display items on the spinner and to perform event handling.

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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[] country = { "A", "B", "C", "D", "E"};
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //Getting the instance of Spinner and applying OnItemSelectedListener on it
       Spinner spin = (Spinner) findViewById(R.id.spinner);
       spin.setOnItemSelectedListener(this);
 
       //Creating the ArrayAdapter instance having the country list
       ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
       aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       //Setting the ArrayAdapter data on the Spinner
       spin.setAdapter(aa);
 
   }
 
   //Performing action onItemSelected and onNothing selected
   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
       Toast.makeText(getApplicationContext(),country[position] , Toast.LENGTH_LONG).show();
   }
   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
       // TODO Auto-generated method stub
   }
}

Output 1:

Output 2:

Output 3:

Content Protection by DMCA.com
Please Share