It's sadly not possible in plain XML, but there's a nice way without using manual Menu.add*
methods: here's how you can obtain a Menu
instance to include/inflate the other file into:
inflater.inflate(R.menu.player, menu);
MenuItem fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu()); // needs <menu />
You can put the above code to any of the following using the specified inflater
:
Activity.onCreateContextMenu(menu, v, menuInfo)
: getMenuInflater()
Fragment.onCreateContextMenu(menu, v, menuInfo)
: getActivity().getMenuInflater()
Activity.onCreateOptionsMenu(menu)
: getMenuInflater()
Fragment.onCreateOptionsMenu(menu, inflater)
: inflater
menu/player.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/teameditor_remove"
android:title="Remove Player from Team"
/>
<item android:id="@+id/teameditor_assignbattingposition"
android:title="Assign Batting Position"
/>
<item android:id="@+id/teameditor_assignfieldingposition"
android:title="Assign Feilding Position">
<menu><!-- include: positions.xml --></menu>
</item>
</menu>
The empty <menu />
placeholder is very important, without that getSubMenu()
will be null
!
menu/positions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/fp_pitcher"
android:title="Pitcher"
/>
<item android:id="@+id/fp_catcher"
android:title="Catcher"
/>
<!-- SNIP --->
</menu>
Note on your onContextItemSelected
idea
I'm thinking that you inflate it in the onContextItemSelected
method [...]
I think it's too late if you're in onContextItemSelected
, since you're already handling the event which would lead to showing you're submenu... which is not inflated yet. You could try the same inflate into getSubMenu()
, but I'm not sure that it'll show up. It's best to create the menu where it's supposed to be created.
Note on including the same submenu multiple times in the same menu
Untested If you need to inflate the same positions.xml
into teameditor_assignbattingposition
as well you'll have some problems in onOptionsItemSelected
/onContextItemSelected
.
One way to work around it is to convert the findItem
variable to a field and save the reference to both
this.fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu());
this.bp_menu = menu.findItem(R.id.teameditor_assignbattingposition);
inflater.inflate(R.menu.positions, bp_menu.getSubMenu());
and then in on*ItemSelected
:
switch (item.getItemId()) {
case R.id.fp_pitcher:
if (item == fp_menu.findItem(R.id.fp_pitcher)) {
// selected inside teameditor_assignfieldingposition
} else if (item == bp_menu.findItem(R.id.fp_picther)) {
// selected inside teameditor_assignbattingposition
} else {
throw new ImLostInMenusException();
}
return true;
}
return super.on*ItemSelected();