Android multilevel spinner is used to select a option from list available, a drop down list is populated when user try to click on the spinner and from which he can select a option.
The selected option can be found at
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { }
Spinner’s are mostly used in category selection, used mostly in forms to fetch the user input so that there is no grammatical errors or different words accepted other than specified as input.
Android Multilevel Spinner :
When we have nested categories in spinner’s we can use multi-level spinner’s for attaining the user input.
Where the second spinner data is loaded from first spinner selected option.
Project Structure :
This image depicts the project implementation of android multilevel spinner.
activity_main.xml :
Add two spinners to the layout file using which we will populate the view
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" android:padding="50dp"> <androidx.appcompat.widget.AppCompatSpinner android:id="@+id/os_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="132dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.505" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatSpinner android:id="@+id/version_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="48dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/os_spinner" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
Add two spinner’s to the activity i.e., operating system and then select the version based on the operating system.
Spinner os_spinner, version_spinner;
And initialize, you can also opt in for latest view binding’s instead of the default way of declaring views.
os_spinner = findViewById(R.id.os_spinner); version_spinner = findViewById(R.id.version_spinner);
setOnClickListeners for the spinners
os_spinner.setOnItemSelectedListener(this); version_spinner.setOnItemSelectedListener(this);
load arraylistData
Here we are adding categories to the second spinner i.e., android, iOS, windows versions
private void loadArrayList() { version_android.add("Cupcake"); version_android.add("Donut"); version_android.add("Eclair"); version_android.add("Froyo"); version_android.add("Gingerbread"); version_android.add("Honeycomb"); version_android.add("Ice Cream Sandwich"); version_android.add("Jelly Bean"); version_android.add("KitKat"); version_android.add("Lollipop"); version_android.add("Marshmallow"); version_android.add("Nougat"); version_android.add("Oreo"); version_android.add("Pie"); version_ios.add("iOS 4"); version_ios.add("iOS 5"); version_ios.add("iOS 6"); version_ios.add("iOS 7"); version_ios.add("iOS 8"); version_ios.add("iOS 9"); version_ios.add("iOS 10"); version_ios.add("iOS 11"); version_ios.add("iOS 12"); version_ios.add("iOS 13"); version_windows.add("Windows 1.0"); version_windows.add("Windows 2.0"); version_windows.add("Windows 3.0"); version_windows.add("Windows 95"); version_windows.add("Windows NT 4.0"); version_windows.add("Windows 98"); version_windows.add("Windows 2000"); version_windows.add("Windows Me"); version_windows.add("Windows XP"); version_windows.add("Windows Vista"); version_windows.add("Windows 7"); version_windows.add("Windows 8"); version_windows.add("Windows 10"); }
Add a adapter
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, os_spin);
Set a simple layout file to the adapter simple_spinner_dropdown_item
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
and set adapter
os_spinner.setAdapter(adapter);
Finally we can get the spinner selected data from a single onItemSelected as below by checking the parent id and comparing with spinner id’s.
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(parent.getId() == R.id.os_spinner) { } else if(parent.getId() == R.id.version_spinner) { } }
To fetch the position of first spinner os_spinner to populate second spinner version_spinner
if(parent.getId() == R.id.os_spinner) { if(position == 0) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_android); } else if(position == 1) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_ios); } else if(position == 2) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_windows); } adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); version_spinner.setAdapter(adapter2); }
And in second spinner listener we can get data
else if(parent.getId() == R.id.version_spinner) { Toast.makeText(this, ""+version_spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); }
Android multilevel spinner Full Code :
Providing the full source code for android multilevel spinner implementation.
package com.abhishek.multilevelspinner; import androidx.appcompat.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; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ Spinner os_spinner, version_spinner; String[] os_spin = { "Android", "iOS", "Windows" }; ArrayList version_android = new ArrayList(); ArrayList version_ios = new ArrayList(); ArrayList version_windows = new ArrayList(); ArrayAdapter adapter2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); os_spinner = findViewById(R.id.os_spinner); version_spinner = findViewById(R.id.version_spinner); loadArrayList(); os_spinner.setOnItemSelectedListener(this); version_spinner.setOnItemSelectedListener(this); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, os_spin); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); os_spinner.setAdapter(adapter); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(parent.getId() == R.id.os_spinner) { if(position == 0) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_android); } else if(position == 1) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_ios); } else if(position == 2) { adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, version_windows); } adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); version_spinner.setAdapter(adapter2); } else if(parent.getId() == R.id.version_spinner) { Toast.makeText(this, ""+version_spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); } } @Override public void onNothingSelected(AdapterView<?> parent) { } private void loadArrayList() { version_android.add("Cupcake"); version_android.add("Donut"); version_android.add("Eclair"); version_android.add("Froyo"); version_android.add("Gingerbread"); version_android.add("Honeycomb"); version_android.add("Ice Cream Sandwich"); version_android.add("Jelly Bean"); version_android.add("KitKat"); version_android.add("Lollipop"); version_android.add("Marshmallow"); version_android.add("Nougat"); version_android.add("Oreo"); version_android.add("Pie"); version_ios.add("iOS 4"); version_ios.add("iOS 5"); version_ios.add("iOS 6"); version_ios.add("iOS 7"); version_ios.add("iOS 8"); version_ios.add("iOS 9"); version_ios.add("iOS 10"); version_ios.add("iOS 11"); version_ios.add("iOS 12"); version_ios.add("iOS 13"); version_windows.add("Windows 1.0"); version_windows.add("Windows 2.0"); version_windows.add("Windows 3.0"); version_windows.add("Windows 95"); version_windows.add("Windows NT 4.0"); version_windows.add("Windows 98"); version_windows.add("Windows 2000"); version_windows.add("Windows Me"); version_windows.add("Windows XP"); version_windows.add("Windows Vista"); version_windows.add("Windows 7"); version_windows.add("Windows 8"); version_windows.add("Windows 10"); } }
Android multilevel spinner output :
The following screen depicts the usage of android multilevel spinner.
If you are having any queries on this tutorial on android multilevel spinner let us know in the comment section below.
Share and like this tutorial for more interesting tutorials.