dynamically enable/disable hide/unhide Android ActionBar action icon
You can do it by overriding the onPrepareOptionsMenu() method. Here is a small example
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (count < 1)
menu.getItem(4).setEnabled(false); //disable menuitem 5
if (!after)
menu.getItem(1).setVisible(false); // invisible menuitem 2
invalidateOptionsMenu();
return true;
}
However, This method is only called whenever you click the menu button in the action bar. If you've any icons on the action bar (except menu button), clicking on that will not trigger the OnPrepareOptionsMenu() method. In order to trigger this method manually you can use the invalidateOptionsMenu() in your methods. like this
void yourMethod () {
...
invalidateOptionsMenu();
...
}
Have a look at this, it shows how to change menu items at runtime.
http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu
You could for example save the menu as a member variable of your Activity inside onCreateOpionsMenu() and then do something like this:
MenuItem item = mMenu.findItem(R.id.addAction);
item.doSomething()
when you want to change something on a specific menu item.