How to dynamically operate items in action bar

Source: Internet
Author: User

/* Called whenever we call invalidateOptionsMenu ()*/

@ Override

Public boolean onPrepareOptionsMenu (Menu menu ){

// If thenav drawer is open, hide action items related to the content

// View

Boolean drawerOpen = mDrawerLayout. isDrawerOpen (mDrawerList );

Menu. findItem (R. id. action_websearch). setVisible (! DrawerOpen );

Log. e (TAG, "onPrepareOptionsMenu ");

/**

* Hide the items in the actionbar

*/

// If (! IconVisibleFlag)

// Menu. findItem (R. id. action_contact_back). setVisible (true );

// Else

// Menu. findItem (R. id. action_contact_back). setVisible (false );

Return super. onPrepareOptionsMenu (menu );

}



Explanation:

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.

@Overridepublic boolean onPrepareOptionsMenu(Menu menu) {    menu.clear();    if(isChangedStat) {        menu.add(0, MENUITEM, 0, "True");    } else {        menu.add(0, MENUITEM, 0, "False");    }    return super.onPrepareOptionsMenu(menu);}

Please note two points

1-If possible just enable or disable menu item or looks possible in your case can change the title of same menu will work because menu. clear (); may need over attention while handling

2-as per link provided by Atlos

On Android 2.3.x and lower, the system callonprepareoptionsmenu () each time the user opens the options menu (presses the Menu button).

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update,you must call invalidateOptionsMenu()To request that the system call onPrepareOptionsMenu ().


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.