This is again another very simple tutorial on using a Spinner View provided by Android SDK. A spinner is supposed to be a view that displays one child at a time (Whatever that means).
This example is very similar to the previous one on AutoCompleteTextView.
Jumping right into the code, here is the layout xml file:
<Spinner
android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop = "true"></Spinner>
Nothing special. It just consists of one element.
Now in the main activity, I create an array of android books:
String[] androidBooks =
{
"Hello, Android - Ed Burnette",
"Professional Android 2 App Dev - Reto Meier",
"Unlocking Android - Frank Ableson",
"Android App Development - Blake Meike",
"Pro Android 2 - Dave MacLean",
"Beginning Android 2 - Mark Murphy",
"Android Programming Tutorials - Mark Murphy",
"Android Wireless App Development - Lauren Darcey",
"Pro Android Games - Vladimir Silva",
};
Then in the onCreate() method, I create an ArrayAdapter that I can pass to this Spinner as the data Source.
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,androidBooks);
Then, I get a handle to the Spinner, and set the ArrayAdapter to it
sp = (Spinner)findViewById(R.id.Spinner01);
sp.setAdapter(adapter);
Now, on selecting one of the items in the spinner, I want to be able to toast a message on the book that was selected. It is done as follows:
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int item = sp.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected the book: " + androidBooks[item],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
That is it. Now execute and see it work. Here is how it would look:
The example code can be downloaded here.
While this blog started off as my personal ramblings on Techncial things it has turned out to be a blog dedicated to Android. Any other technical ramblings are shared at my Technical Blog.
NOTE:
NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.
Thanks
License

Sai Geetha's Blog by Sai Geetha M N is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Search This Blog
x
Showing posts with label Spinner. Show all posts
Showing posts with label Spinner. Show all posts
Wednesday, 12 May 2010
Subscribe to:
Posts (Atom)