Tuesday 14 April 2015

Dynamically update option menu items in Android.

Updating Menu Items Dynamically


By overriding your Activity’s onPrepareOptionsMenu method, you can modify a Menu based on an application’s current state immediately before the Menu is displayed.

This lets you dynamically disable/enable Menu Items, set visibility, and modify text.

Note that the onPrepareOptionsMenu method is triggered whenever the menu button is clicked, the overflow menu displayed, or the Action Bar is created.


To modify Menu Items dynamically, you can either record a reference to them from within the onCreateOptionsMenu method when they’re created, or you can use the findItem method on the Menu object, where onPrepareOptionsMenu is overridden.

Modifying Menu Items dynamically


@Override 
public boolean onPrepareOptionsMenu(Menu menu) 
{  
     super.onPrepareOptionsMenu(menu);
  
           MenuItem menuItem = menu.findItem(MENU_ITEM);
           [ ... modify Menu Items ... ]
 
    return true; 
}

No comments:

Post a Comment