Reputation: 706
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??
Upvotes: 0
Views: 502
Reputation: 348
try this
mToolbar.getMenu().findItem(id).setEnabled(false);
I hope it will work for you.
Upvotes: 1
Reputation: 2216
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;
}
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>
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);
Upvotes: 0
Reputation: 5861
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;
}
Upvotes: 4
Reputation: 1821
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);
Upvotes: 0