Thursday, June 04, 2015

Adding a dropdown (spinner) to toolbar in Android app

Here is a very elegant and simple solution on how to add a Spinner (a dropdown) to a toolbar in an Android app.
Add a Spinner as menu item and set the action view class

<menu xmlns:android=""
xmlns:yourapp="" >

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

Then handle the contents in onCreateMenuItem, as usual.

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
    s.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection


No comments: