Add/Remove items at RunTime in Spinner Android?[Solved]

By | October 5, 2015

Step 1: Add the following Code to activity_main.xml


<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.example.spinner_example.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="" />

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:prompt="@string/spinner_title"
 android:layout_marginTop="81dp" />

 <Button
 android:id="@+id/add_btn"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="300dp"
 android:text="+" 
 android:onClick="add_item"
 />

 <Button
 android:id="@+id/remove_btn"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/add_btn"
 android:layout_alignBottom="@+id/add_btn"
 android:layout_alignParentRight="true"
 android:onClick="remove_item"
 android:text="-" />

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/spinner1"
 android:layout_below="@+id/spinner1"
 android:layout_marginTop="27dp"
 android:hint="Enter Item"
 android:ems="10" >

 <requestFocus />
 </EditText>

</RelativeLayout>

Step 2: Add the following code to mainActivity.java

package com.example.spinner_example;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
 private Spinner spinner;
 private List<String> list;
 private ArrayAdapter<String> adapter;
 private TextView textView;
 private EditText editText;
 private String editText_text,selected_item;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 editText= (EditText) findViewById(R.id.editText1);
 spinner = (Spinner) findViewById(R.id.spinner1);
 textView =(TextView) findViewById(R.id.textView1);
 list=new ArrayList<String>();
 list.add("Surat"); list.add("Nagaur");list.add("Delhi");
 add_spinner();
 }
public void add_spinner()
{
 
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Spinner click listener
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
 public void add_item(View v)
 {
 editText_text= editText.getText().toString();
 if(editText_text.length()<1)
 {
 Toast.makeText(this,"Please enter item ",Toast.LENGTH_SHORT).show();
 }
 else
 {
 list.add(editText_text);
 textView.setText("Added : "+editText_text);
 add_spinner();
 editText.setText(null);
 }
 
 }
 public void remove_item(View v)
 {
 
 list.remove(selected_item);
 textView.setText("Removed : " + selected_item);
 add_spinner();
 }

 @Override
 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
 long arg3) {
 // TODO Auto-generated method stub
 spinner.setSelection(arg2);
 selected_item = (String) spinner.getSelectedItem();
 
 }

 @Override
 public void onNothingSelected(AdapterView<?> arg0) {
 // TODO Auto-generated method stub
 
 }
}

Step 3 : Now you can save and execute the Application. Your output will be like this :
Screenshot_2015-10-04-10-34-38

1,735 total views, 1 views today

(Visited 662 times, 1 visits today)