I want to add a submenu inside my OptionsMenu to a menuItem, programatically according to my parameters. I\'ve checked \"MenuItem\" in android sdk and there is no addSubMenu
Sometimes Android weirdness is really amazing (and amusing..). I solved it this way:
a) Define in XML a submenu placeholder like this:
<item android:visible="true" android:id="@+id/m_area"
android:titleCondensed="Areas"
android:title="Areas"
android:icon="@drawable/restaur"
android:enabled="true">
<menu>
<item android:id="@+id/item1" android:title="Placeholder"></item>
</menu>
</item>
b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mapoptions, menu);
int idx=0;
SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
subm.clear(); // delete place holder
while(true)
{
anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
if(anarea == null)
break;
subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
++idx;
}
}
The best way to do this is in your xml menu file. You can do this by creating a new menu
object inside of an item
:
<menu>
<item>
...
<menu>
...
</menu>
...
</item>
</menu>
I would just create the submenu in xml file, and in run time get the submenu from menu object, (using findItem(id) method) and use submenu.setVisible(boolean) to add/remove it on run time.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu1" android:alphabeticShortcut="a"
android:title="Menu No. 1" android:orderInCategory="1" />
<item android:id="@+id/menu2" android:alphabeticShortcut="b"
android:title="Menu No. 2" android:orderInCategory="2">
<menu >
<group android:id="@+id/group2" android:checkableBehavior="single">
<item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
<item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
</group>
</menu>
</item>
To provide a comprehensive example of Phil's answer, here is my complete, working XML for a menu with two choices, each of which is a menu with three choices. I intend to add a third menu to the top level ...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/Examine"
android:title="@string/Examine"
HTMLCode:showAsAction="always">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/load"
android:title="@string/load"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/findfirst"
android:title="@string/findfirst"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/findnext"
android:title="@string/FindNext"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
<item android:id="@+id/Redirect"
android:title="@string/Redirect"
HTMLCode:showAsAction="ifRoom|withText">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/getRedirect"
android:title="@string/getRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/toggleRedirect"
android:title="@string/toggleRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/copyRedirect"
android:title="@string/copyRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
</menu>
I know this is an old question, but I just came across this problem myself. The most straightforward way of doing this, seems to be to simply specify the item itself as a submenu, then add to this item. E.g.:
menu.add(groupId, MENU_VIEW, Menu.NONE, getText(R.string.menu_view));
menu.add(groupId, MENU_EDIT, Menu.NONE, getText(R.string.menu_edit));
SubMenu sub=menu.addSubMenu(groupId, MENU_SORT, Menu.NONE, getText(R.string.menu_sort));
sub.add(groupId, MENU_SORT_BY_NAME, Menu.NONE, getText(R.string.menu_sort_by_name));
sub.add(groupId, MENU_SORT_BY_ADDRESS, Menu.NONE, getText(R.string.menu_sort_by_address));
:
: