Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a common menu item in multiple menus in Android menu xml?

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.

like image 331
Tima Avatar asked Dec 02 '10 16:12

Tima


People also ask

What is onCreateOptionsMenu in android?

The intent of implementing this method is to populate the menu passed with the items you define in the R. menu. game_menu layout file. #Java @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().

What is contextual menu in android?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

How to customize Android menus?

Like any other UI component, even Android menus can be customized with the help of attributes like- It uniquely identifies the item of the menu. It sets an icon to represent the item. It sets the title of the item. It specifies when and how this item should appear as an action item in the app bar.

How do you define a menu in XML?

Defining a Menu in XML. For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity's code, you should define a menu and all its items in an XML menu resource. You can then inflate the menu resource (load it as a Menu object) in your activity or fragment.

Can I add a menu in Java instead of XML?

If creating menus in XML is not suitable, you can add menu items in Java instead. Providing options and customizations to the user in your Android application is absolutely necessary if you want to make a useful Android app. So have fun building powerful, extensible Android apps with simple, easily-editable menus.

What is options menu in Android app?

Options menu and app bar. The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings.".


1 Answers

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.

like image 116
azelez Avatar answered Oct 02 '22 02:10

azelez