Android Adding a Submenu to a Menuitem, Where Is Addsubmenu()

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;
}
}

How to programmatically add a submenu item to the new material design android support library

[Update 20-03-2016]
Bug is resolved. So no need to worry.

[This below contetnt is outdated.]

Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I have report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..

    navView = (NavigationView) findViewById(R.id.navView);
Menu m = navView.getMenu();
SubMenu topChannelMenu = m.addSubMenu("Top Channels");
topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");

After adding your menu just write below code ..

    MenuItem mi = m.getItem(m.size()-1);
mi.setTitle(mi.getTitle());

It's currently hacky solution. But work for me ...

[update 26-06-2015]

As I have reported this bug at Android Bug source Now Bug is marked as a future release, here is the link https://code.google.com/p/android/issues/detail?id=176300

So we can say that the bug is no more exist on future library. So you don't have to use tricky solution. i will also update this answer again when Future version release number is maintion for this bug.

Create Sub Menu in Android through Code

This Code is Working

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

/**
* Class which shows how to work with Submenus
*
* @author FaYna Soft Labs
*/
public class Main extends Activity {

private static final int FILE = 0;
private static final int EDIT = 1;

private static final int NEW_MENU_ITEM = Menu.FIRST;
private static final int SAVE_MENU_ITEM = NEW_MENU_ITEM + 1;

private static final int UNDO_MENU_ITEM = SAVE_MENU_ITEM + 1;
private static final int REDO_MENU_ITEM = UNDO_MENU_ITEM + 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fileMenu = menu.addSubMenu("File");
SubMenu editMenu = menu.addSubMenu("Edit");
fileMenu.add(FILE, NEW_MENU_ITEM, 0, "new");
fileMenu.add(FILE, SAVE_MENU_ITEM, 1, "save");
editMenu.add(EDIT, UNDO_MENU_ITEM, 0, "undo");
editMenu.add(EDIT, REDO_MENU_ITEM, 1, "redo");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case NEW_MENU_ITEM:
showMsg("New");
break;
case SAVE_MENU_ITEM:
showMsg("Save");
break;
case UNDO_MENU_ITEM:
showMsg("Undo");
break;
case REDO_MENU_ITEM:
showMsg("Redo");
break;
}
return super.onOptionsItemSelected(item);
}

private void showMsg(String message) {
Toast msg = Toast.makeText(Main.this, message, Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
msg.getYOffset() / 2);
msg.show();
}
}


Related Topics



Leave a reply



Submit