Absolutely the best and and the simplest answer I found so far is here.
Basically, no need for custom layout in this case. Just set the actonViewClass:
<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" /> <== this is all that's required
</menu>
And then handle it in onCreateOptionsMenu, as usual:
@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); // get the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(onItemSelectedListener);
This is by far the simplest and cleanest solution. Credits to François Poyer, the original author.