Vikas Vikas - 1 year ago 257
Android Question

Android: How to enable/disable option menu item on button click?

I can easily do it when I am using


But I have a button somewhere in screen, and on clicking that button, it should enable/disable context menu items.


Answer Source

Anyway, the documentation covers all the things.

Changing menu items at runtime

Once the activity is created, the onCreateOptionsMenu() method is called only once, as described above. The system keeps and re-uses the Menu you define in this method until your activity is destroyed. If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method. This passes you the Menu object as it currently exists. This is useful if you'd like to remove, add, disable, or enable menu items depending on the current state of your application.


public boolean onPrepareOptionsMenu (Menu menu) {
    if (isFinalized) {
        // You can also use something like:
        // menu.findItem(;
    return true;

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().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download