How to add a Dropdown item on the action bar
Asked Answered
C

3

82

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?

Calvados answered 29/11, 2011 at 14:23 Comment(1)
L
223

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

Landslide answered 29/11, 2011 at 15:3 Comment(10)
Great answer! Btw., you can also include icons in the submenu.Lyckman
how do I respond to a click of the spinner?Choriamb
This does not work for Android 2.2. The app is crashing. Do you have a solution?Leontine
Use ActionBarSherlock...they hadn't introduced ActionBar back in Android 2.2 and therefore ActionBarSherlock allows you to back date these features. actionbarsherlock.comCoenurus
Is it possible to set customLayout and Submenu at the same time, Im trying it for long timeCappello
what happen if you are in tab navigation mode?Selectivity
what is '@array/order' this here and what we can add?Sob
Thank you very much,Working fine, But my question is how to handle OnItemSelectedListener for this spinner?Bromic
@Sandeep Maram You can get spinner by Spinner spinner = (Spinner)menu.findItem(R.id.menuPeriodType).getActionView() in onCreateOptionsMenu(). Then set the item select listener.Gyrfalcon
The first option doesn't seem to work as of Android 7.0.Healall
P
36

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.

Pali answered 4/6, 2015 at 8:57 Comment(1)
Tested and approved.Connacht
R
0

It will work as dropdown only

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!--<item-->
    <!--android:id="@+id/save_contact"-->
    <!--android:icon="@drawable/edit_new"-->
    <!--android:title="Save Contact"-->
    <!--app:showAsAction="never" />-->

    <item
        android:id="@+id/send_money"
        android:icon="@drawable/edit_new"
        android:title="Send Money"
        app:showAsAction="never" />

    <item
        android:id="@+id/request_money"
        android:icon="@drawable/edit_new"
        android:title="Request money"
        app:showAsAction="never" />

    <item
        android:id="@+id/recharge"
        android:icon="@drawable/edit_new"
        android:title="Recharge"
        app:showAsAction="never" />
</menu>

inside fragment

setHasOptionsMenu(true)

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.chat_details_menu, menu);


    super.onCreateOptionsMenu(menu, inflater);
}
Rouault answered 28/7, 2016 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.