Add item to spinner dynamically in android using Edittext at app run time

Insert item value elements with use of EditText and add button click in android.

In this tutorial we are adding items inside Spinner programmatically using EditText so when app developer start typing on edittext and click on add button then automatically the entered value will insert into spinner string list. So here is the complete step by step tutorial for Add item to spinner dynamically in android using Edittext at app run time.

android-project-download-code-button

How to Add item to spinner dynamically in android using Edittext at app run time.

Code for MainActivity.java file.

 package com.additemtospinner_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.EditText;
import android.widget.Spinner;
import android.widget.Toast;

 public class MainActivity extends Activity {
 Spinner SPINNER;
 Button ADD;
 EditText EDITTEXT;
 String[] spinnerItems = new String[]{
 "ANDROID",
 "PHP"
 
 };
 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);
 ADD = (Button)findViewById(R.id.button1);
 EDITTEXT = (EditText)findViewById(R.id.editText1);
 
 stringlist = new ArrayList<>(Arrays.asList(spinnerItems));
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this,R.layout.textview,stringlist );
 
 arrayadapter.setDropDownViewResource(R.layout.textview);
 
 SPINNER.setAdapter(arrayadapter);
 
 ADD.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 GETTEXT = EDITTEXT.getText().toString();
 
 stringlist.add(GETTEXT);
 
 arrayadapter.notifyDataSetChanged();
 
 Toast.makeText(MainActivity.this, "Item Added", 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.additemtospinner_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
 android:ems="10"
 android:hint="Type Elements" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/editText1"
 android:layout_alignBottom="@+id/editText1"
 android:layout_alignRight="@+id/spinner1"
 android:text="ADD" />

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/editText1"
 android:layout_below="@+id/editText1" />

</RelativeLayout>

Code for textview.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="#026bcd" />

Screenshot before item add:

before-item-add

Screenshot after item add :

Add item to spinner dynamically in android using Edittext at app run time

Click here to download Add item to spinner dynamically in android using Edittext at app run time project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *