Disable Action Bar button in Android-ThrowExceptions

Exception or error:

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.

How to solve:
  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);
    

###

A simple method to disable a button in the action bar is to save a reference to the menu in onCreateOptionsMenu():

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);    
    mMenu = menu;    // Save reference
    return super.onCreateOptionsMenu(menu);
}

and then call setEnabled() on the menu item to enable or disable:

mMenu.findItem(R.id.my_action_item).setEnabled(false);

No need to re-create the menu by calling invalidateOptionsMenu().

###

For Android developers who are using a custom ActionBar i.e (Save & Cancel action buttons), I have tried the following code to disable Cancel action button as follows:

Java code:

LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View actionBarButtons = inflater.inflate(
                R.layout.edit_event_custom_actionbar, new LinearLayout(this),
                false);

        View cancelActionView = actionBarButtons
                .findViewById(R.id.action_cancel);
        cancelActionView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        cancelActionView.setEnabled(false);

        View doneActionView = actionBarButtons.findViewById(R.id.action_done);
        doneActionView.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setCustomView(actionBarButtons);

edit_event_custom_actionbar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?attr/dividerVertical"
    android:dividerPadding="12dp"
    android:orientation="horizontal"
    android:showDividers="middle" >

    <!-- id must match corresponding menu item id -->
<TextView  
    android:id="@+id/action_cancel"
    style="@style/EditEventCustomActionButton"
    android:text="@string/cancel_label" 
    android:drawableLeft="@drawable/ic_action_cancel" android:padding="5dp" />

    <!-- id must match corresponding menu item id -->
<TextView  
    android:id="@+id/action_done"
    style="@style/EditEventCustomActionButton"
    android:text="@string/abc_action_mode_done" 
    android:drawableLeft="@drawable/abc_ic_cab_done_holo_dark" android:paddingRight="5dp" />

</LinearLayout>

And add this code to your /values/styles.xml:

<style name="EditEventCustomActionButton" parent="android:style/Widget.Holo.Light.ActionButton">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:focusable">true</item>
    <item name="android:textColor">#FFFFFF</item>
</style>

###

You can do something like that:

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    menu.findItem(R.id.your_item).setEnabled(false);
    return super.onCreateOptionsMenu(menu);
}

###

In case you want to disable the button when it’s tapped, it’s as easy as:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    item.isEnabled = false
}

Leave a Reply

Your email address will not be published. Required fields are marked *