35

Many of menus have always one item same to all.

Is there the way to define that item as extra menu and include it to all other?

Something like this:

menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_main"
          android:title="@string/text_mainmenu" />
</menu>

menu/other.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      parent="@menu/main">
    <item android:id="@+id/menu_other"
          android:title="@string/text_othermenu" />
</menu>

I know, that it's possible to do it programmaticaly, but I thought xml-way is nicer.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Tima
  • 12,765
  • 23
  • 82
  • 125

2 Answers2

70

Inflating each menu and calling to super works great! Here is an example:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    getMenuInflater().inflate(R.menu.other, menu);
    return true;
}

You can control the order if super also adds more items by calling it before/after other inflates, or not call at all it to ignore those items.

stkent
  • 19,772
  • 14
  • 85
  • 111
azelez
  • 2,501
  • 2
  • 27
  • 26
  • 1
    Working great for me as well - in my case I'm putting the super at the end of `onCreateOptionsMenu()` so that the "different" menu options show at the top of the menu instead of at the bottom. – George Feb 21 '13 at 05:56
  • 6
    Use android:orderInCategory to have an explicit order of items, which is _independent_ of the inflation order. – Bananeweizen Feb 07 '15 at 17:00
  • Very helpful. An alternative usage of this capability is to have Activity B extend Activity A -- each with its own menu. In this situation Activity B will have the menu options its menu defines combined with those defined by the menu used by Activity A. – dazed Dec 16 '15 at 11:29
  • 1
    Awesome, someone actually putting up an example of doing it, so many android menu examples and all of them seem to `super() clear() inflate()` which defeats the purpose of having object hierarchies. – ebyrob Nov 10 '16 at 21:37
13

AFAIK, <include> only works for layout XML, not menu XML, though you can certainly give it a try. Lacking that, the programmatic option (inflating both menu XML files) is the only option I am aware of.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    as i wrote about programmatic way, I meant a bit different thing, but then I saw, i can inflate each single menu in my activity and so to create custom menus from some menu-stones. Thank you for bringing me to this idea. – Tima Dec 06 '10 at 17:02
  • 1
    @Tima Genius! Didn't think of inflating more than one menu XML. That being said, I think it's odd `include` and `merge` are only supported in layouts, but hey, what do I know. – Joshua Pinter May 09 '14 at 02:03
  • Little code example might be nice, I don't often see inflate happening more than once on a given menu without clear(). – ebyrob Nov 10 '16 at 21:36
  • @ebyrob, Common rarely give code examples for whatever reason..At least in these old android dev questions – lasec0203 Jul 19 '22 at 16:29