Android: How to enable/disable option menu item on button click?
Anyway, the documentation covers all the things.
Changing menu items at runtime
Once the activity is created, the
onCreateOptionsMenu()
method is called
only once, as described above. The
system keeps and re-uses theMenu
you
define in this method until your
activity is destroyed. If you want to
change the Options Menu any time after
it's first created, you must override
theonPrepareOptionsMenu()
method.
This passes you the Menu object as it
currently exists. This is useful if
you'd like to remove, add, disable, or
enable menu items depending on the
current state of your application.
E.g.
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (isFinalized) {
menu.getItem(1).setEnabled(false);
// You can also use something like:
// menu.findItem(R.id.example_foobar).setEnabled(false);
}
return true;
}
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu()
to request that the system call onPrepareOptionsMenu()
.
Enable/Disable ActionBar Menu Item
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addprojectmenu, menu);
menu.getItem(0).setEnabled(false); // here pass the index of save menu item
return super.onPrepareOptionsMenu(menu);
}
Just inflate it on prepare time and disable after inflated menu no need to inflate
in oncreateoptionemenu
time or you can just use last two line of code after inflating from onCreateOptionMenu
.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setEnabled(false); // here pass the index of save menu item
return super.onPrepareOptionsMenu(menu);
}
How to disable menu button and the action bar still remains there
Hardware menu button is not controlled by onPrepareOptionsMenu().
Generally speaking, it is not good practice to change the behavior of hardware buttons because users expect it to behave a certain way (which I believe is to expand the overflow menu).
If you absolutely have to disable it, you could listen for it to be pressed in our Activities like this:
public boolean dispatchKeyEvent(KeyEvent event) {
final int keycode = event.getKeyCode();
final int action = event.getAction();
if (keycode == KeyEvent.KEYCODE_MENU && action == KeyEvent.ACTION_UP) {
return true; // consume the key press
}
return super.dispatchKeyEvent(event);
}
how to disable multiple clicks on menu option in android
You can set the visibility or enable/disable the item by code.
MenuItem item = menu.findItem(R.id.your_item);
item.setVisible(true);
item.setEnabled(false);
Of course you have to check somewhere whether to enable oder disable the icon.
android - Enable and Disable MenuItem on Navigation Drawer Activity Layout
You can access menu from NavigationView.getMenu()
Below is code :
NavigationView navigationView= findViewById(R.id.nav_id_in_layout)
Menu menuNav=navigationView.getMenu();
MenuItem nav_item2 = menuNav.findItem(R.id.nav_item2);
nav_item2.setEnabled(false)
Related Topics
How to Create an Android View Pager With a Dots Indicator
How to Display a Loading Spinner While Getting Data from Firestore
How to Set the Starting Position of a Scrollview
How to Use Adb With Emulator Like Memu/Bluestacks/Nox App Player
Retrofit Body Giving Null Values But Coming Perfect in Postman
How to Change Listview Height Dynamically in Android
Changing Background Color of the Layout on a Button Click in Android
How to Show a Video Thumbnail from a Video Path
Resolving Problems With Android Studio Freezing Constantly
How to Center Items of a Recyclerview
Super.Onbackpressed() and Finish() Exits the App
Retrofit @Body With @Multipart Having Issue
Where Is Adb.Exe in Windows 10 Located
How to Format Credit Card Expiry Date in Mm/Yy When Entered by User