Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Action Bar button in Android

Tags:

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.

like image 424
Mediha Avatar asked Jul 18 '13 13:07

Mediha


People also ask

How do I get rid of action bar?

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.

How do I remove the back button from my Toolbar?

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.

How can I customize my action bar in Android?

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.


1 Answers

  1. Once you perform the user action when you want to disable the action bar, set some flag, say disableButtonFlag.

  2. Call invalidateOptionsMenu(). This will trigger onCreateOptionsMenu to be invoked to regenerate your menu.

  3. 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);
    
like image 148
Vrashabh Irde Avatar answered Oct 12 '22 12:10

Vrashabh Irde