Thursday, August 13, 2015

How to remove an item from Spinner programmatically in Android

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[]{
                "Laceflower",
                "Sugar maple",
                "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_spinner_dropdown_item,
                plantsList
        );


        // Set the drop down view resource
        spinnerArrayAdapter.setDropDownViewResource(
                android.R.layout.simple_dropdown_item_1line
        );

        // Finally, data bind the spinner object with adapter
        spinner.setAdapter(spinnerArrayAdapter);


        // Set the button click listener
        button.setOnClickListener(v -> {
            // Remove item/element from List index zero
            // Remove first item of List
            if (!plantsList.isEmpty()){
                plantsList.remove(0);
                spinnerArrayAdapter.notifyDataSetChanged();
            }
        });
    }
}
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="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove First Item From 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>