Is it possible to disable a button in Action Bar in Android? I was looking around and I could not find any code snippet for that, I was thinking that there should be some easy way.
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.
Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.
This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Once you perform the user action when you want to disable the action bar, set some flag, say disableButtonFlag
.
Call invalidateOptionsMenu()
. This will trigger onCreateOptionsMenu
to be invoked to regenerate your menu.
Finally modify your onCreateOptionsMenu
to disable the button you want depending on the state of disableButtonFlag
.
if (disableButtonFlag) {
menu.findItem(R.id.your_item).setEnabled(false);
} else {
menu.findItem(R.id.your_item).setEnabled(true);
}
Or more simply:
menu.findItem(R.id.your_item).setEnabled(!disableButtonFlag);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With