Remove item from spinner in android programmatically

How to delete specific item using position inside spinner and update spinner again at run time.

Spinner items can be easily deletable when we are using list<string> to store string array inside it. So here is the complete step by step tutorial for Remove item from spinner in android programmatically.

android-project-download-code-button

How to Remove item from spinner in android programmatically.

Code for MainActivity.java file.

 package com.removeitemspinner_android_examples.com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

 public class MainActivity extends Activity {
 Spinner SPINNER;
 Button REMOVE;
 String[] spinnerItems = new String[]{
 "ANDROID",
 "PHP",
 "BLOGGER",
 "SEO",
 "WORDPRESS"
 
 };
 String GETTEXT;
 List<String> stringlist;
 ArrayAdapter<String> arrayadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SPINNER = (Spinner)findViewById(R.id.spinner1);
 REMOVE = (Button)findViewById(R.id.button1);
 
 stringlist = new ArrayList<>(Arrays.asList(spinnerItems));
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this,R.layout.display_spinner,stringlist );
 
 arrayadapter.setDropDownViewResource(R.layout.display_spinner);
 
 SPINNER.setAdapter(arrayadapter);
 
 REMOVE.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 stringlist.remove(2);
 
 arrayadapter.notifyDataSetChanged();
 
 Toast.makeText(MainActivity.this, "Item Remove", Toast.LENGTH_LONG).show(); 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.removeitemspinner_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/spinner1"

 android:text="Remove BLOGGER From Spinner" />

</RelativeLayout>

Code for display_spinner.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="250dp"
 android:layout_height="wrap_content"
 android:textColor="#fefcfe"
 android:padding="10dp"
 android:textSize="18dp"
 android:background="#8802cd" />

Screenshot before delete element :

spinner-item-delete

Screenshot after delete element:

Remove item from spinner in android programmatically

Click here to download Remove item from spinner in android programmatically project with source code.