Skip to content Skip to sidebar Skip to footer

How We Can Add Menu Item Dynamically

hi frnds am creating an application which is a tab application. in my Home which extends sherlockFragmentActivity, i am inflating menu.xml and includes code for on optionMenuitem c

Solution 1:

Try the following way.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, 0, 0, "Option1").setShortcut('3', 'c');
        menu.add(0, 1, 0, "Option2").setShortcut('3', 'c');
        menu.add(0, 2, 0, "Option3").setShortcut('4', 's');

        SubMenu sMenu = menu.addSubMenu(0, 3, 0, "SubMenu"); //If you want to add submenu               
        sMenu.add(0, 4, 0, "SubOption1").setShortcut('5', 'z');
        sMenu.add(0, 5, 0, "SubOption2").setShortcut('5', 'z');             

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {           

        switch (item.getItemId()) {
        case 0:
            // code for option1
            return true;
        case 1:
            // code for option2
            return true;
        case 2:
            // code for option3
            return true;            
        case 4:
            // code for subOption1
            return true;
        case 5:
            // code for subOption2
            return true;            
        }
        return super.onOptionsItemSelected(item);
    }

This may help you.


Solution 2:

In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.

<item
    android:id="@+id/action_newItem"
    android:icon="@drawable/action_newItem"
    android:showAsAction="never"
    android:visible="false"
    android:title="@string/action_newItem"/>

Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remoev) by overriding onPrepareOptionsMenumethod available in Fragment.

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.action_newItem).setVisible(true);
    super.onPrepareOptionsMenu(menu);

}

Solution 3:

here is an example...it might help you...

  private static final int MENU_ADD = Menu.FIRST;
    private static final int MENU_LIST = MENU.FIRST + 1;
    private static final int MENU_REFRESH = MENU.FIRST + 2;
    private static final int MENU_LOGIN = MENU.FIRST + 3;


  @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();
            if(enableAdd)
                menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
            if(enableList)
                menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
            if(enableRefresh)
                menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
            if(enableLogin)
                menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            super.onOptionsItemSelected(item);

            switch (item.getItemId()) {
            case MENU_ADD: doAddStuff(); break;
            case MENU_LIST: doListStuff(); break;
            case MENU_REFRESH: doRefreshStuff(); break;
            case MENU_LOGIN: doLoginStuff(); break;
            }
            return false;

Solution 4:

based on Gunaseelan answer

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    menu.removeGroup(1);
    if (iotsnames.isEmpty()) return true;
    for (int i=0; i<iotsnames.size(); i++ ){
        menu.add(1, i, 0, iotsnames.get(i) );
    }
    return true;
}

Solution 5:

use onPrepareOptionsMenu method and clear all menu first using

menu.clear();

then add menu.

Also refer here.. check its onPrepareOptionsMenu method


Post a Comment for "How We Can Add Menu Item Dynamically"