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

Search This Blog

Loading...
x

Wednesday, 12 May 2010

Spinner View | Android Beginner Dev Tutorial

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.

30 comments:

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. Thanks 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:
    http://www.katr.com/article_android_spinner01.php

    ReplyDelete
  4. 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

    ReplyDelete
  5. Source code id Deleted. can u please check it again....

    ReplyDelete
  6. Hi,

    I have uploaded the source code once again. Please check if you are able to download it now.

    ReplyDelete
  7. hi sai geetha ,
    thanks 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

    ReplyDelete
  8. hai mam ,
    i'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

    ReplyDelete
  9. Hi mam..
    I'm sarvesh I want to add large no text in spinner in multiline concept can you please help me

    Your truly
    sarvesh

    ReplyDelete
  10. hi mam
    I'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

    ReplyDelete
  11. Hi

    after 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.

    ReplyDelete
  12. how can i solve my problem kindly give reply.

    ReplyDelete
  13. Good 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.


    Thanks,
    Harsha

    ReplyDelete
  14. nice tutorial, can you show me for custom spinner?

    ReplyDelete
  15. Hai Geeta,

    Can you please guide me on creating relative spinner( ie if i change the country list then city list to be populated in another spinner)

    ReplyDelete
  16. Hey thanks, How can we create spinners programmatically? and populate the value from Database, any idea of how to do that?

    ReplyDelete
  17. Hi Saigeetha,
    i 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 .

    ReplyDelete
  18. Hi Saigeetha,
    Thanks 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.

    ReplyDelete
  19. SHOW THE @!#%$ IMPORTS PEOPLE.

    ReplyDelete
  20. Hello ,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
  21. @Sam

    You 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.

    ReplyDelete
  22. Hi,

    I'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

    ReplyDelete
  23. Nice tutorial,Very Useful.

    ReplyDelete
  24. AnyOne 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.

    Thanks in advance.

    ReplyDelete
  25. Thanks for this! God bless :)

    ReplyDelete
  26. Madam,

    This is Naga raju, I am new to android your blog is very nice and it is very helpful to me.

    thankyou mam

    ReplyDelete
  27. Hi Geetha,
    This 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 ?

    ReplyDelete
  28. Hi,
    I wanted to take training on webservices from you how could i want to contact with you...

    ReplyDelete
  29. Hi

    Thank 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

    ReplyDelete