简体   繁体   中英

Can't hide actionbar menu item programatically

I'm trying to hide an Actionbar menu item if a shared preference is false.

I'm getting the shared preference as I want, but the menu item wont hide.

// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.activity_main_actionbar);       

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

What am i doing wrong??

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;
}

try this

mToolbar.getMenu().findItem(id).setEnabled(false);

I hope it will work for you.

try this

invalidateOptionsMenu();

I hope it is helpful for you.

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);

  1. Start by saving the menu globally in your activity-

     Menu menuu; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. menuu=menu; getMenuInflater().inflate(R.menu.menu_main, menu); return true; } 
  2. Have different menu.xml for you . one is having noitem and other is containing the items you want.

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.afixi.prasenjeetpati.notification_service.MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>

menu_blank.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.afixi.prasenjeetpati.notification_service.MainActivity">
</menu>
  1. Now to remove the option menu at any time do this-

     menuu.clear(); getMenuInflater().inflate(R.menu.menu_blank, menuu); 

    and to get back the normal menu-

     menuu.clear(); getMenuInflater().inflate(R.menu.menu_main, menuu); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM