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
Wednesday, 12 May 2010
Spinner View | Android Beginner Dev Tutorial
Labels:
Android,
Android Basics,
Android Examples,
Spinner,
Views
Subscribe to:
Post Comments (Atom)
Source code for this example is not available to download. By the way your tutorials are really very effective. Thanks for such a nice tutorials.
ReplyDeleteSource code for this example is not available to download. By the way your tutorials are really very effective. Thanks for such a nice tutorials.
ReplyDeleteThanks for the cool tutorial. I've written another tutorial that your readers may be interested in about using custom java objects as the data source for spinner controls. You can find that tutorial here:
ReplyDeletehttp://www.katr.com/article_android_spinner01.php
Hi! nice tutorial, when i tap the spinner, it shows the menu in the center of the screen, what if i want to show the menu exactly below the spinner widget? I want to get that effect. Please help me out
ReplyDeleteSource code id Deleted. can u please check it again....
ReplyDeleteHi,
ReplyDeleteI have uploaded the source code once again. Please check if you are able to download it now.
hi sai geetha ,
ReplyDeletethanks for this sample code
can you please help me to create 3 spinner
1 spinner is loaded from ksoap array return and second, third one filled on clicked or select from spinner 1
i need this i have create this but getting error from ksoap i mean empty listview
thanks
hai mam ,
ReplyDeletei'm vijay from chenai.nw i'm begining stage for android developing.ur tutorials very useful for me.,thank u very mam........
yours truly,
vijay
Hi mam..
ReplyDeleteI'm sarvesh I want to add large no text in spinner in multiline concept can you please help me
Your truly
sarvesh
hi mam
ReplyDeleteI'm sarvesh i want to use push notification in my app and I need help for this.
can you please provide the code for push notification with how n where to use.
your truly
Sarvesh
Hi
ReplyDeleteafter selecting the spinner item it needs to enter into the next view but when i run my app for the first time directly it's jumping into the nextview.
how can i solve my problem kindly give reply.
ReplyDeleteGood tutorial, can we set the custom adapter which has multiple columns, in way I want the list view to be displayed with icons and names.
ReplyDeleteThanks,
Harsha
nice tutorial, can you show me for custom spinner?
ReplyDeleteHai Geeta,
ReplyDeleteCan you please guide me on creating relative spinner( ie if i change the country list then city list to be populated in another spinner)
Hey thanks, How can we create spinners programmatically? and populate the value from Database, any idea of how to do that?
ReplyDeleteHi Saigeetha,
ReplyDeletei developing apps for Android Tablet in that three images swipe horizontally and on OnClick of that image corresponding sound should come there are 300 images each three image comes at a time and swipe it and next three image should come,please help me i want to complete it soon,help me as soon as possible its my humble request to you .
hi
ReplyDeleteHi Saigeetha,
ReplyDeleteThanks for posting and explaining this tutorial. However, I have one question for you on Spinner. I want to loop the the items in the spinner and select a particular Item.
For eg; lets say i have few items in the spinner namely {Red, Green, Blue, Black, White, Orange}
How do I manually set the spinner selected index on White?
Your reply will be much appreciated. Thanks in advance and keep up the good work.
SHOW THE @!#%$ IMPORTS PEOPLE.
ReplyDeleteHello ,Thanks For posting the nice tutorials.i want to ask from u that if i am having more than one spinner i want to know that on which spinner i have clicked 1 spinner 2 spinner 3 etc.Can u help me on this....?
ReplyDelete@Sam
ReplyDeleteYou can create Anonymous InnerType, in this way each spinner will have there own clicklistener implementation.
That is what Sai Geetha has written in this post.
Hi,
ReplyDeleteI've seen so many examples of how to create A spinner, but could you demonstrate how to create multiple spinners, with the data from spinner2 dependant upon the selection from spinner1?
It would be so useful.
Thanks
Nice tutorial,Very Useful.
ReplyDeleteAnyOne know How to set zero position null in Spinner. fill data by dynamically. same as this tutorial image but 1st "Hello Android" rather than null value.
ReplyDeleteThanks in advance.
Thanks for this! God bless :)
ReplyDeleteMadam,
ReplyDeleteThis is Naga raju, I am new to android your blog is very nice and it is very helpful to me.
thankyou mam
Hi Geetha,
ReplyDeleteThis example works fine with version 2.2 and 4.0. But if I want to use the spinner in 4.0 and have the same effect in version 2.2 then it doesn't work.
Is it possible to use spinner in version 4.0 and then have the same effect when we run that on android version 2.2 ?
Hi,
ReplyDeleteI wanted to take training on webservices from you how could i want to contact with you...
Hi
ReplyDeleteThank you for this tutorial on Android spinners. I have an Android Spinner View board on Verious with this post. I'd like to share it with anyone interested in creating a simple Spinner View. Please let me know if there’s anything I can add to make a more comprehensive resource for other developers.
http://www.verious.com/board/Giancarlo-Leonio/creating-an-android-spinner-view-from-developers-like-mkyong-and-sai-geetha
@Veriously