Code This

std::cout <<me.ramble() <<std::endl;

A Spinner Control For Android With Multi-Select Support

with 21 comments

I recently had a feature request for Checkout Tracker to allow the user to select multiple order states and multiple items when filtering the order list. I thought to myself, no problem, I can probably bang that out in a half hour or less, right? All I need is to do is add support for multiple states & items in the filter restrictions, and turn on a setting that allows the user to select multiple items in the Spinner controls used in the interface. Wrong. The Android Spinner control does not support multiple selection. This seems like a fairly obvious, and fairly common, use case. Unfortunately many such use cases are not covered currently by the Android libraries. Such is the curse of working with a young framework. No matter, I set out to create my own version of this control.

This took me about a day, after which I was able to bang out the feature request in under a half hour. Here are some screens:

 

I have made this control available for all the world to use, on github. Hopefully I have saved you a day’s worth of work.

Feel free to leave feedback about the API or functionality, or request changes.

Advertisements

Written by Kris Wong

June 17, 2012 at 11:49 am

21 Responses

Subscribe to comments with RSS.

  1. Thanks for taking the time to share your work. Unfortunately for me, too many blanks are left to be filled and I was not able to use your code.

    cabow

    August 20, 2012 at 11:49 am

    • I am not sure what you mean by “too many blanks are left to be filled.”

      Kris Wong

      August 20, 2012 at 11:52 am

  2. How do you get an item/id from this, if you used a String[]?

    Michelle L

    October 9, 2012 at 1:51 pm

    • You may use either getSelectedStrings or getSelectedIndicies.

      Kris Wong

      October 9, 2012 at 3:47 pm

      • I need a specify index, not a list of index. Is this possible. Thanks.

        Michelle L

        October 9, 2012 at 3:48 pm

      • I am not sure what situation would allow for getting 1 selected index, but not a list of them. The list contains indicies, after all. Just get the first index in the list.

        Kris Wong

        October 9, 2012 at 3:51 pm

  3. MultiSelectSpinner mySpin = (MultiSelectSpinner)findViewById(R.id.spinner1);

    Caused by: java.lang.ClassCastException: android.widget.Spinner cannot be cast to com.example.testspinner.MultiSelectSpinner

    why?

    myElog

    February 16, 2013 at 3:50 am

    • You have used the built-in Android Spinner in your layout XML. You need to use the fully-qualified class name for the multi-select spinner instead. In your case this is com.example.testspinner.MultiSelectSpinner.

      Kris Wong

      February 16, 2013 at 10:44 am

      • how to use the fully classified class name for the MultiSelectSpinner.
        I did like this :
        com.example.i317194.multiselect.MultiSelectSpinner spin = (com.example.i317194.multiselect.MultiSelectSpinner) findViewById(R.id.spinner);

        But its not working.

        Atul Kashyap

        February 18, 2015 at 5:13 am

  4. Thanks for sharing this, I almost wanted to write one myself but thought that some kind soul must have already done this and sharing it to the world so I did some googling.. glad that I did :)

    I will show you my app once it is out… then you will be seeing your work in action :)

    Bruce

    July 6, 2013 at 6:03 pm

    • I’m glad that you found it useful.

      Kris Wong

      July 6, 2013 at 6:05 pm

  5. Kris tells me that I can not cast a widget spinner to multiselectspinner. Error: can not be cast to android.widget.Spinner com.vm.MultiSelectSpinner

    • MultiSelectSpinner is not related in any way to android.widget.Spinner.

      Kris Wong

      July 31, 2013 at 12:05 pm

  6. great work, Kris, thanks!

    Jarrette Schule

    December 5, 2013 at 11:48 am

    • Kris, what’s the best way to implement a hint when you first display the MultiSelectSpinner? I’m not using labels with my controls so it’s just blank until you tap…

      Jarrette Schule

      December 5, 2013 at 1:04 pm

  7. Is there a way to view the selection as a list of views? Preferably non-scrollable. (I.e. after you’ve selected the items and have pressed OK)

    Carlo Chum

    June 14, 2014 at 11:47 am

  8. Excellent code, thanks a lot for sharing with us. Is there any way to have 1 or more options selected by default?

    Chang

    March 6, 2015 at 1:01 pm

  9. hi, is it easy to use this and adding a “all” and “none” checkbox?

    Jacopo

    September 30, 2015 at 11:07 am

  10. hi, good day :) Can you show how to display those selected itemS to edittext?

    Ann

    August 7, 2016 at 6:10 pm

  11. can you show how to display to edittext the multiple items selcted
    from spinner

    Ann

    August 7, 2016 at 7:34 pm

  12. hi kris,you did a nice job,i want to fetch the selected item from spinner
    and display in a edittext

    Pavithra Cs

    February 21, 2017 at 2:21 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: