2

I want to add the menu items dynamically. I have menu like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/allItems"
        android:showAsAction="always"
        android:title="@string/trackAll"/>
    <item
        android:id="@+id/specficItems"
        android:showAsAction="always"
        android:title="@string/trackSpecific"/>
    <item
        android:id="@+id/favItems"
        android:showAsAction="always"
        android:title="@string/trackFav"/>

</menu>

I try to add items into favItems. But when I try like this :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.admin_menu, menu);

    MenuItem item=menu.findItem(R.id.favItems);

    menu.add(0, MY_MENU_1, 0, "Item 1").setShortcut('3', 'c');
    menu.add(0, MY_MENU_2, 0, "Item 2").setShortcut('4', 's');
    menu.add(0, MY_MENU_3, 0, "Item 3").setShortcut('5', 'z');
    return true;
}

The output is coming like this: enter image description here

But I want to add this items into favItems

Please let me know any idea to add the items dynamically in favItems.

If I try like this:

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {    
    menu.add(0, MY_MENU_1, 0, "Item 1").setShortcut('3', 'c');
    menu.add(0, MY_MENU_2, 0, "Item 2").setShortcut('4', 's');
    menu.add(0, MY_MENU_3, 0, "Item 3").setShortcut('5', 'z');
    return super.onPrepareOptionsMenu(menu);
    }

The op coming like this:

enter image description here

Vijay
  • 3,152
  • 3
  • 24
  • 33

2 Answers2

1

Right now you are just adding items to the menu.

Try doing this:

 MenuItem item = menu.findItem(R.id.favItems);
 SubMenu sub = item.getSubMenu();

 sub.add(0, MY_MENU_1, 0, "Item 1").setShortcut('3', 'c');
 sub.add(0, MY_MENU_2, 0, "Item 2").setShortcut('4', 's');
 sub.add(0, MY_MENU_3, 0, "Item 3").setShortcut('5', 'z');

Edit:

Also your favItems should be something like this:

<item
    android:id="@+id/favItems"
    android:showAsAction="always"
    android:title="@string/trackFav">
    <menu>
    </menu>  
</item>
Rene Juuse
  • 575
  • 8
  • 16
  • Yes.. This what I want.. Thank you @ Rane Juuse.. But on this have to add ` ` this line in menu XML. – Vijay Apr 20 '15 at 09:44
0

use: onPrepareOptionsMenu(Menu menu)

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

// Gets called everytime, when menu button is pressed
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear(); //Empty's your menu
    menu.add();  // adding your item

    super.onPrepareOptionsMenu(menu);
    return true;
}

edit: to add one item because you did something before... you could use:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(IWantToAddMyItem){
        menu.add(); // your item
        IWantToAddMyItem = false;
    }

    super.onPrepareOptionsMenu(menu);
    return true;
}
Strider
  • 4,452
  • 3
  • 24
  • 35
  • I have update the question with your solution result. Please refer this and let me, where I did mistake? – Vijay Apr 20 '15 at 09:27
  • you need to use `menu.clear()` (clears the menu) and than use `menu.add()` to add your desired items – Strider Apr 20 '15 at 09:30