How to add submenu items to ActionBar action in code?

Yes, there is.

The addSubMenu method returns a SubMenu object. A SubMenu is also a Menu, so you can call add on it to add items to the submenu rather than the parent menu. Your code above is creating two different submenus for Form 1 and Form 2 rather than two items within a single New Form submenu.

Example:

SubMenu submenu = menu.addSubMenu(0, Menu.NONE, 1, "New Form").setIcon(R.drawable.ic_new_form);
submenu.add("Form 1").setIcon(R.drawable.attachment);

Leave a Comment