Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we can add menu item dynamically

Tags:

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 click listener. The Fragmentactivity contains tabhost and on each tab it load fragments. this is my menu.xml

<item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="always"
       android:icon="@drawable/setting_selector"
        android:title=""
        >
        <menu >
            <item
                android:id="@+id/Profile"
                android:showAsAction="ifRoom"
                android:title="Profile"/>
            <item
                android:id="@+id/chngDoctor"
                android:showAsAction="ifRoom"
                android:title="Change doctor"
                android:visible="false"/>
            <item
                android:id="@+id/changePword"
                android:showAsAction="ifRoom"
                android:title="Change password"/>
            <item
                android:id="@+id/logout"
                android:showAsAction="ifRoom"
                android:title="Logout"/>
        </menu>
    </item>

and this is my onCreateOptionMenu and onOptionItemSelected methods in class Home

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.main, menu);
    SubMenu subMenu = (SubMenu) menu.getItem(0).getSubMenu();
    if(userType.equals("admin"))
        subMenu.getItem(1).setVisible(true);
    else
        subMenu.getItem(1).setVisible(false);
    return true;
}

and this is my onOptionItemSelected method

  @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) {
        case R.id.Profile:
              break;
        case R.id.changePword :
            break;
        case R.id.chngDoctor :
                 break;
        case R.id.logout:
            Home.this.finish();
            break;
        }
        return true;
    }

i need to add some menus depending on tab change. that is on tab change i load different fragments and when fragment changes i need to add new items to the menu. my ListFrag which extends SherlockFragment and it will load when i click on the 3 rd tab. when this fragment load i need to add 1 menu item to the menu

like image 604
Vikky Avatar asked Jun 26 '13 04:06

Vikky


People also ask

How do you create a dynamic menu?

How to Dynamically Add Menu Items to an Android Activity. The following specific example adds a MENU_LOGOUT option if the user is logged in. private static final int MENU_LOGOUT = MENU. FIRST + 4; public boolean onPrepareOptionsMenu(Menu menu) { ... if(auth.

How do I add a menu item?

On the Navigation page, click the title of the menu that you want to edit. Click Add menu item. Enter a name for the menu item. This name displays in the menu, and can include special characters or emoji.

What is a dynamic menu?

The Dynamic Menu is a modified concept of the Menu component, used when the number of actions available to the user is dynamic or variable. It can also be useful when the number of actions in the menu is large enough that a search functionality would be required.

What callback method is used to create menu items?

Inside the onCreateContextMenu() callback method, you can add menu items using one of the add() methods, or by inflating a menu resource that was defined in XML. Then, register a ContextMenu for the View, with registerForContextMenu() .


2 Answers

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.

like image 162
Gunaseelan Avatar answered Sep 22 '22 09:09

Gunaseelan


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);

}
like image 25
Parinda Rajapaksha Avatar answered Sep 21 '22 09:09

Parinda Rajapaksha