Android adding a submenu to a menuItem, where is addSubMenu()? Android adding a submenu to a menuItem, where is addSubMenu()? android android

Android adding a submenu to a menuItem, where is 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;            }}


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));::


Here's a complete answer which builds on the idea of using a placeholder but uses mostly xml to add the submenu.

If you have a menu like so called main_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:title="My Menu"    android:id="@+id/my_menu_item">    <!-- A empty SubMenu -->    <menu></menu></item></menu>

Create another menu sub_menu.xml which will be used in my_menu_item:

<menu xmlns:android="http://schemas.android.com/apk/res/android">  <item android:title="SubMenu One"    android:id="@+id/submenu_one" />  <item android:title="SubMenu Two"    android:id="@+id/submenu_two" />  <item android:title="SubMenu Three"    android:id="@+id/submenu_three" /></menu>

In your onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {   // Inflate your main_menu into the menu   getMenuInflater().inflate(R.menu.main_menu, menu);   // Find the menuItem to add your SubMenu   MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);   // Inflating the sub_menu menu this way, will add its menu items    // to the empty SubMenu you created in the xml   getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());}

This solution is nice since the inflater handles most of the work.