1

I need to add a spinner to the top menu bar in my Android app as seen below:

enter image description here

Before Android 5.0, I simply used:

final android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false); // DEPRACATED
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)

but that method was deprecated. I have gone through all the samples at: http://developer.android.com/samples/ui.html but not a single one had a spinner/dropdown menu to base my code off of.

Given NAVIGATION_MODE_LIST is deprecated, how can I add a spinner that will be compatible with Android 5.0, and all the previous versions?

UPDATE: After adding the tool bar as can be seen in my code, the error occurs as it notes it cannot see the import even though android.support.v7.widget.Toolbar; is clearly listed

enter image description here

Sauron
  • 6,399
  • 14
  • 71
  • 136

1 Answers1

6

You're going to need to add a Spinner to the Toolbar:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary">

    <Spinner
        android:id="@+id/spinner_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

You will then need to disable the default title:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

// You can then retrieve and setup the Spinner as needed in your Activity/Fragment.

From this answer

Android Lollipop, add popup menu from title in toolbar

Community
  • 1
  • 1
Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • This will not work, if I add ToolBar, it forces the MinSdk version to be 21, not compatible with any previous version. My Android phone, running 4.4, won't even install it – Sauron Dec 16 '14 at 03:12
  • You need to use the support library. Check the import android.support.v7.widget.Toolbar – Robert Estivill Dec 16 '14 at 03:15
  • That still does not work. I updated my question as I added the toolbar and view but still has errors, please view – Sauron Dec 16 '14 at 04:17
  • Take a look at OneWay's comment, you need to cast the View to ToolBar – Robert Estivill Dec 16 '14 at 12:42