Add An Item To Spinner Programmatically
Spinner is a single selection list widget in native Android SDK. The Spinner widget displays an item in its visible area and the remaining items are shown on a drop-down menu after clicking on the Spinner widget itself. Android app users can change their selection by exploring the hidden items from the drop-down menu and clicking an item from them.
Android app developers can put a Spinner element in their app user interface by adding a Spinner tag in their XML layout file. But populating a Spinner widget with items is a little bit harder. To do this, android app developers have to bind an array adapter with the Spinner widget. An array adapter gets its items from an array list and an array list can be populated from an array.
When a Spinner data bind with an array adapter, The Spinner element shows items on the app interface. One item is visible and the rest items are inside a drop-down menu. The Spinner items are prepopulated on activity start. So, is there any way to add an item dynamically to a Spinner items collection? This android app development tutorial will demonstrate to you how can we add an item to the Spinner widget programmatically on a Button click event.
In the first step, we will add a Spinner widget and a Button widget to the XMl layout file. On the Button click event, we will call the ArrayList add() method to add a new item to the array list. We know the array adapter is built by the array list and the Spinner widget gets its items from the array adapter, so when we add an item to the array list, it also adds to the Spinner items collection.
Adding an item is done, now somehow we have to inform the Spinner widget about the change we made. ArrayAdapter notifyDataSetChange() method allows us to inform the Spinner widget about the adapter change. So the Spinner widget updates its items collection in runtime and displays it in the drop-down menu.
Android app developers can put a Spinner element in their app user interface by adding a Spinner tag in their XML layout file. But populating a Spinner widget with items is a little bit harder. To do this, android app developers have to bind an array adapter with the Spinner widget. An array adapter gets its items from an array list and an array list can be populated from an array.
When a Spinner data bind with an array adapter, The Spinner element shows items on the app interface. One item is visible and the rest items are inside a drop-down menu. The Spinner items are prepopulated on activity start. So, is there any way to add an item dynamically to a Spinner items collection? This android app development tutorial will demonstrate to you how can we add an item to the Spinner widget programmatically on a Button click event.
In the first step, we will add a Spinner widget and a Button widget to the XMl layout file. On the Button click event, we will call the ArrayList add() method to add a new item to the array list. We know the array adapter is built by the array list and the Spinner widget gets its items from the array adapter, so when we add an item to the array list, it also adds to the Spinner items collection.
Adding an item is done, now somehow we have to inform the Spinner widget about the change we made. ArrayAdapter notifyDataSetChange() method allows us to inform the Spinner widget about the adapter change. So the Spinner widget updates its items collection in runtime and displays it in the drop-down menu.
MainActivity.java
package com.cfsuman.androidtutorials;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
Spinner spinner = findViewById(R.id.spinner);
// Initializing a String Array
String[] plants = new String[]{
"California sycamore",
"Mountain mahogany",
"Butterfly weed",
"Carrot weed"
};
// Convert array to a list
List<String> plantsList = new ArrayList<>
(Arrays.asList(plants));
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter
= new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
plantsList
);
// Set the drop down view resource
spinnerArrayAdapter.setDropDownViewResource(
android.R.layout.simple_dropdown_item_1line
);
// Set a button click listener
button.setOnClickListener(view -> {
// Add the item to the spinner items source
plantsList.add("Apple");
// Notify the array adapter for the change
spinnerArrayAdapter.notifyDataSetChanged();
// Disable the button itself
view.setEnabled(false);
});
// Finally, data bind the spinner object with adapter
spinner.setAdapter(spinnerArrayAdapter);
}
}
activity_main.xml
<?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"
android:background="#DCDCDC"
android:padding="32dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add 'Apple' To Spinner"
android:textAllCaps="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>


- How to change EditText font size in Android
- How to set default text on EditText in Android
- How to disable EditText in Android
- How to remove auto focus from EditText in Android
- How to convert EditText text to all uppercase in Android
- How to change EditText border color in Android
- How to show a Toast message from a Fragment in Android
- How to create AlertDialog with theme in Android
- How to dismiss AlertDialog in Android
- How to create a Yes/No AlertDialog in Android
- How to display item list in an AlertDialog in Android
- How to set Spinner alternate item color in Android
- How to disable an item in Spinner in Android
- How to set selected item of Spinner programmatically in Android
- How to remove an item from Spinner programmatically in Android