dynamically enable/disable hide/unhide Android ActionBar action icon

点点圈 提交于 2019-12-04 04:55:25

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.

As I understand, you need to remove the icon that shows on the action bar top left.

You can do that with this simple line of code:

final ActionBar actionBar = getActionBar();     
actionBar.setDisplayShowTitleEnabled(false);

And if you like to get rid of the app name try:

actionBar.setDisplayShowHomeEnabled(false);

Hope this helps.

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();
...
}
Felipe R Valera

You can also try this:

final ActionBar actionBar= getActionBar();
actionBar.hide();

It will hide the icon and title.

Don't forget your import:

import android.app.ActionBar;

Good Luck

Try ActionBar.getActionBar.hide();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!