Android Lollipop, add popup menu from title in toolbar

Matt Wear picture Matt Wear · Oct 22, 2014 · Viewed 34k times · Source

I'm unable to see how adding a popup menu from the title is accomplished like is shown in many of the material design examples. Any help would be much appreciated.

Toolbar Popup From Title

Answer

Chris Banes picture Chris Banes · Oct 22, 2014

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 = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

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