Creating submenu dynamically

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Creating submenu dynamically

Postby finucas » Fri Jul 16, 2010 7:35 pm

Hi i having an issue figuring out how to create a submenu and affix it to a menu item.

I can do this easily enough in my menu.xml file and all works well
but the submenu items are not known at run-time so i cannot do it this way,

for demonstration purposes heres a menu.xml file
consisting of two menu items

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
<item android:id="@+id/add_menu"
<item android:id="@+id/cancel_menu"

Now in my main code how would i go about adding a submenu for say add_menu?

the thing is i know how to add a SubMenu in the code w/o using xml menu file also
but i dont know how to create one when i have the menu defined in xml?

Maybe do i have to give the menu an ID in the xml,
then addSubMenu in the java code to that specific ID?

Please help i have been searching for hours now.
Posts: 6
Joined: Tue Jul 13, 2010 4:26 pm


Re: Creating submenu dynamically

Postby andreslr » Sat Mar 12, 2011 10:35 am

Did you solve this? I'm trying to achieve the same.
Junior Developer
Junior Developer
Posts: 12
Joined: Wed Jul 01, 2009 6:44 pm


Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests