12

I have added a spinner to my ActionBar using the second option from the answer here .

How to I add a spinner adapter to the spinner? I tried using a Spinner object as Google describes here but get a null Spinner object.

Anybody know how to do this? I don't want the spinner to be in the navigation area of the action bar but in with the other action items (I am using the split action bar).

Thanks for the help!

Community
  • 1
  • 1
user1509130
  • 195
  • 1
  • 2
  • 9

4 Answers4

33

I know this is an old question, but just in case someone stumbles on it (as I did) and still looks for a complete answer, here is how to do it using the compatibility library, so that it works from to v7 (Android 2.1 Eclair) to current v19 (Android 4.4 KitKat):

In menu_layout.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

  <item android:id="@+id/spinner"
    yourapp:showAsAction="ifRoom"
    yourapp:actionViewClass="android.widget.Spinner" />
</menu>

Using http://schemas.android.com/apk/res-auto namespace aliased as yourapp enables you to use the attributes showAsAction and actionViewClass that do not exist on older versions of Android.

Then in your Activity code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_layout, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
    spinner.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection

Et voilà!

silver est
  • 51
  • 6
François POYER
  • 471
  • 4
  • 6
  • 1
    If you dont want to worry about older versions then yourapp namespace is not required, you can work with android namespace. Also, this works perfectly well with CursorLoaders. – lalitm Apr 24 '14 at 05:07
  • Since at the moment Android 2.3.3 (API v10) still represent ~18% of the android market (see the [Android Dashboard](http://developer.android.com/about/dashboards/index.html) for current numbers), I thought it was important to provides the compatibility answer. But you are correct that the android namespace is enough if your application minimum target sdk is above Android 3.0 (API v11). – François POYER Apr 28 '14 at 13:02
  • @lalitm if you noticed, it's for the compatibility library. Definitely required if you are aiming to be BC. – frostymarvelous Feb 17 '16 at 13:50
  • A warning about this: testing this on a Nexus 6 with Android version 6.0.1 - I found that if there is not room for the Spinner on the ActionBar - it won't present the choices. It doesn't work on the normal menu position. Reference to showAsAction setting. So on small screens it could end up being a broken control. – RoundSparrow hilltx Mar 05 '16 at 13:26
  • GOOD NEWS: showAsAction="ifRoom|collapseActionView" seems to work properly on both the menu and action bar... I recommend that combination in case your user has a small screen. – RoundSparrow hilltx Mar 05 '16 at 13:43
  • If your spinner is still null and you're using AppCompat version 7, then initialize it using `app:actionViewClass="android.support.v7.widget.AppCompatSpinner"` – Zon May 10 '18 at 12:33
15

I know you ditched the spinner, but I'll give some hints here in case other people is having the same problem or you come to develop the same pattern in a different app

  • If you got null is because you didn't properly specify the IDs. Double check the IDs.
  • on of the links you showed over complicated stuff by specifying a actionLayout that is just a spinner, you can just specify an actionViewClass="android.widget.Spinner" that will do the trick.
  • then in the OnCreateOptionsMenu you do:

    inflater.inflate(R.menu.my_menu, menu); // inflate the menu
    Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView(); // find the spinner
    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar()
            .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray
    s.setAdapter(mSpinnerAdapter); // set the adapter
    s.setOnItemSelectedListener(myChangeListener); // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
    

happy coding...

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Any idea how this would work for a custom adapter? I'm using a images instead of text words to display in the Spinner, so the line `SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar() .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item);` must be different for a custom adapter. Can't figure out how. – Azurespot May 25 '15 at 09:45
  • just invoke whatever constructor you have on your custom adapter. `SpinnerAdapter mSpinnerAdapter = new MyCustomAdapter()` – Budius May 25 '15 at 09:47
  • Thanks Budius, I did do that, but for some reason the dropdown does not happen. I figured it was because `android.R.layout.simple_spinner_dropdown_item` is missing in my custom adapter (since it uses a Spinner row as ImageView, a place where the drawables array goes into). But not sure if that's the problem. If you wanted to check out my code: http://stackoverflow.com/questions/30433501/custom-spinner-not-showing-dropdown-in-actionbar-icon The question is still unanswered. Thanks. – Azurespot May 25 '15 at 10:05
6

Well, I ditched the Spinner idea for using a submenu. I realized that the spinner was for selecting things that stayed selected; submenus seamed to be a better UI fit.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
user1509130
  • 195
  • 1
  • 2
  • 9
-1
inflater.inflate(R.menu.my_menu, menu); // inflate the menu 

Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView();     // find the spinner 
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar() .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item);    // create the adapter from a StringArray 
s.setAdapter(mSpinnerAdapter);   // set the adapter 
s.setOnItemSelectedListener(myChangeListener);    // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
Saif
  • 6,804
  • 8
  • 40
  • 61