Dynamically adding items to Spinner Widget in Android

August 1, 2012
By

In this article we are creating an application in which user inputs items to a Spinner widget via an EditText widget.

The application described here is developed using Eclipse 3.7.2 with ADT plugin ( 20.0.2 ) and Android SDK ( R20.0.1 )


1. Create a new Android application project namely “AddItemsDynamicallySpinner”

Create new Android application Project

Figure 1 : Create new Android application Project


2. Design application launcher

Design Application Launcher

Figure 2 : Design Application Launcher


3. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity


4. Enter MainActivity details

Enter MainActivity Details

Figure 3 : Enter MainActivity Details


5. Update the file res/values/strings.xml


<resources>

    <string name="app_name">AddItemsDynamicallySpinner</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="hintTxtItem">Enter an item here ...</string>
    <string name="lblBtnAdd">Add Item</string>

</resources>


6. Update the layout file res/layout/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" >

    <EditText
        android:id="@+id/txtItem"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/hintTxtItem"
    />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblBtnAdd"
        android:layout_toRightOf="@id/txtItem"
    />

    <Spinner
        android:id="@+android:id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
    />

</RelativeLayout>


7. Update the file MainActivity.java


package in.wptrafficanalyzer.additemsdynamicallyspinner;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends Activity {

    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList<String> list = new ArrayList<String>();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Defining the ArrayAdapter to set items to Spinner Widget */
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

        /** Defining click event listener for the button */
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText txtItem = (EditText) findViewById(R.id.txtItem);
                list.add(txtItem.getText().toString());
                txtItem.setText("");
                adapter.notifyDataSetChanged();
            }
        };

        /** Getting a reference to button object of the resource activity_main */
        Button btnAdd = (Button) findViewById(R.id.btnAdd);

        /** Setting click listener for the button */
        btnAdd.setOnClickListener(listener);

        /** Getting a reference to Spinner object of the resource activity_main */
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        /** Setting the adapter to the ListView */
        spinner.setAdapter(adapter);

        /** Adding radio buttons for the spinner items */
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


8. Application in Execution

Screenshot of the application

Figure 5 : Screenshot of the application


9. Download


10. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , ,

5 Responses to Dynamically adding items to Spinner Widget in Android

  1. nir on August 25, 2012 at 12:25 am

    ty very much!!

  2. Ayfer Kaya on April 16, 2013 at 6:21 pm

    Thank you so much :)

  3. Ivan on October 17, 2013 at 10:17 pm

    Have you ever tried adding it to a actionbarsherlock Navigation dropdown spinner? If so please share as this would be great for my app & give it a cleaner look.

  4. gaurav on April 8, 2015 at 2:01 pm

    i cn do the above program of spinner bt,
    aftr leaving the application and coming back i cant see the items added previous to the spinner,,
    after coming back i want the previous added items in d spinner ,;(

  5. kavya on May 27, 2015 at 10:01 am

    Hi,

    how can i use dynamic spinner without using add button in android.

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner