Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic control of action/menu items in ActionBar

Is there a way to dynamically disable , hide, add/remove menu items in ActionBar ? For example, an action is disabled until user fills a valid phone number in an activity.

I didn't find any useful methods in ActionBar API, the only way seems to be using a custom View in ActionBar.

like image 494
S.D. Avatar asked Sep 14 '12 06:09

S.D.


1 Answers

To tell ActionBar to refresh its menu items: invalidateOptionsMenu()

then to enable/disable Menu Items:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item= menu.findItem(R.id.men_1);
    //depending on your conditions, either enable/disable
    item.setEnabled(false);
    super.onPrepareOptionsMenu(menu);
    return true;
}

and to hide the action bar you have:

getActionBar().hide();
like image 55
Archie.bpgc Avatar answered Sep 19 '22 11:09

Archie.bpgc