Android Spinner Example

In Android, Spinner is nothing but a combo box or list box.

It lets you viewing multiple items and allows you to select one item from the list.

Edit Your XML code like this

[sourcecode language="css"]
<Spinner android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
[/sourcecode]

Your Java Class code should look like this

[sourcecode language="java"]
public class SpinnerExample extends Activity {
private String array_spinner[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array_spinner=new String[5];
array_spinner[0]="1";
array_spinner[1]="2";
array_spinner[2]="3";
array_spinner[3]="4";
array_spinner[4]="5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}
}
[/sourcecode]

The Output will look like
spinnerexample

Comments

Hi nedra, You can't able to

Hi nedra,
You can't able to select more then one item from list.
Try to use Listview with multiple choices, you can get what you except.
See the androidpeople.com after couple of days for that post.
questions ?

Hi nedra, I too mentioned

Hi nedra,
I too mentioned spinner only not list.
we can't able to select more then one item from spinner at a time.
So try to use Listview with multiple choice items.
With in Monday i will post about the Listview with multiple choice select example.

Hi nedra, Please see the

Hi nedra,
Please see the below link
you will get how to select multiple choice in listview http://www.androidpeople.com/android-listview-mul...

Adam, See the below link that

Adam,
See the below link that helps you to add or remove values in run time. http://www.designerandroid.com/?p=8

By using this example you can easily achieve what you expect. When you are selecting the state spinner at the time you can able to change the city spinner value easily.

Questions ?.

Hi vikram, Sysout the array

Hi vikram,

Sysout the array list and make sure its not empty before passing it to spinner.