Search code examples
androidandroid-actionbar

dynamically enable/disable hide/unhide Android ActionBar action icon


I am developing an Android application that employs a standard ActionBar.
on a certain screen i have a Filter icon that is conditionally required,
depending on characteristics of the data being displayed, e.g. some data is filterable
Others not.
i cannot find any methods on actionbar that look likely candidates for programmaticaly hiding an actionbar action icon. how can i enable/disable an actionbar action icon?
Or
how can i hide/unhide an actionbar action icon?


Solution

  • 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.