How to Hide a Menu Item in the Actionbar

How do I hide a menu item in the actionbar?

Get a MenuItem pointing to such item, call setVisible on it to adjust its visibility and then call invalidateOptionsMenu() on your activity so the ActionBar menu is adjusted accordingly.

Update: A MenuItem is not a regular view that's part of your layout. Its something special, completely different. Your code returns null for item and that's causing the crash. What you need instead is to do:

MenuItem item = menu.findItem(R.id.addAction);

Here is the sequence in which you should call:
first call invalidateOptionsMenu() and then inside onCreateOptionsMenu(Menu) obtain a reference to the MenuItem (by calling menu.findItem()) and call setVisible() on it

How to hide menu item in android action bar?

You can get a reference to the menu items which you would like to hide and show in onCreateOptionsMenu and then make one visible and the other invisible inside onOptionsItemSelected :

private MenuItem itemToHide;
private MenuItem itemToShow;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
itemToHide = menu.findItem(R.id.item_to_hide);
itemToShow = menu.findItem(R.id.item_to_show);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_next:
// hide the menu item
itemToHide.setVisible(false);
// show the menu item
itemToShow.setVisible(true);
return true;
}

return super.onOptionsItemSelected(item);
}

Hide/Show Action Bar Option Menu Item for different fragments

This is one way of doing this:

add a "group" to your menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<group
android:id="@+id/main_menu_group">
<item android:id="@+id/done_item"
android:title="..."
android:icon="..."
android:showAsAction="..."/>
</group>
</menu>

then, add a

Menu menu;

variable to your activity and set it in your override of onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// inflate your menu here
}

After, add and use this function to your activity when you'd like to show/hide the menu:

public void showOverflowMenu(boolean showMenu){
if(menu == null)
return;
menu.setGroupVisible(R.id.main_menu_group, showMenu);
}

I am not saying this is the best/only way, but it works well for me.

Trying to hide and show menu items on action bar

Its not enough to change the isDown variable. You have to call the setVisible() method every time you want to change the visibility. That method does more than just setting a boolean value, so just changing a boolean value will not do.

After changing the isDown value to false, you need to call invalidateOptionsMenu() which will re-launch the menu by calling onPrepareOptionsMenu() again.

Try this code for making the menu items unvisible:

...
isdown = false;
invalidateOptionsMenu();
...

Can't hide actionbar menu item programatically

The right place for doing this is onPrepareOptionsMenu. From the docs,

Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.

So, I would recommend you to override onPrepareOptionsMenu and then check for the Shared Prefs inside it and show menu accordingly. Something like,

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Enable disable set start page item
if(!sharedPref.getBoolean("enable_custom_startpage", false)) {
toolbar.getMenu().findItem(R.id.setasstartpage).setVisible(false);
}

return true;
}

how to hide/remove icon menu items in actionbar when moving bottomnavigation kotlin?

You just need to make your FavoriteFragment options menu aware and then in onCreateOptionsMenu, simply clear the menu. this can be done by updating FavoriteFragment with following changes

In onCreateView, call setHasOptionsMenu(true) to receive options menu related callbacks in Fragment

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Allows your fragment to receive options menu related callbacks (onCreateOptionsMenu etc)
setHasOptionsMenu(true)
...
}

now override onCreateOptionsMenu and clear the already populated menu as

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
menu.clear()
}

Hide menu options in action bar


@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);

MenuItem item3 = menu.findItem(R.id.ID OF MENU);
item3.setVisible(false);
}


Related Topics



Leave a reply



Submit