How to add a Dropdown item on the action bar

Paulo Barros picture Paulo Barros · Nov 29, 2011 · Viewed 97.9k times · Source

In my Android Honeycomb application I use Tabs as the navigation style. I would like to add one item next to the overflow button, but I want that item to be a dropdown list, and the user will be able to select an option there, but not related to navigation. What is the easiest way since I'm using mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

Is it possible to do it without using a custom view?

Answer

fhucho picture fhucho · Nov 29, 2011

First option:

menu/options.xml:

<item
    android:icon="@drawable/ic_menu_sort"
    android:showAsAction="ifRoom">
    <menu>
        <item
            android:id="@+id/menuSortNewest"
            android:title="Sort by newest" />
        <item
            android:id="@+id/menuSortRating"
            android:title="Sort by rating" />
    </menu>
</item>

Second option:

menu/options.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menuSort"
        android:showAsAction="ifRoom"
        android:actionLayout="@layout/action_sort"  />
</menu>

layout/action_sort.xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_menu_refresh"
    android:entries="@array/order" />

Docs for menu resources - http://developer.android.com/guide/topics/resources/menu-resource.html