Adding spinner to ActionBar (not Navigation

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=""
xmlns:yourapp="" >

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

Using 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:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    MenuItem item = menu.findItem(;
    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Ă !

Leave a Comment