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”
2. Design application launcher
3. Create a blank activity
4. 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
9. Download

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

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
ty very much!!
Thank you so much
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.
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 ,;(
Hi,
how can i use dynamic spinner without using add button in android.