Disable Action Bar button in Android
Solution 1
Once you perform the user action when you want to disable the action bar, set some flag, say
disableButtonFlag
.Call
invalidateOptionsMenu()
. This will triggeronCreateOptionsMenu
to be invoked to regenerate your menu.Finally modify your
onCreateOptionsMenu
to disable the button you want depending on the state ofdisableButtonFlag
.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);
Solution 2
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()
.
Solution 3
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>
Related videos on Youtube
Mediha
Updated on November 15, 2020Comments
-
Mediha over 3 years
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.
-
Mediha almost 11 yearsBut I need ActionBar, I have requirements for the application. I need to continue using ActionBar and when the user adds one file I need to disable adding more files (button for adding files is in ActionBar).
-
Mediha almost 11 yearsBut it should be enabled at the beginning, I need to disable later, after user action. Please read the comment above. Thanks.
-
Vrashabh Irde almost 11 yearsOh disable button, edited. I assumed remove the action bar, generally what disable the action bar seems to imply generically.
-
Brtle almost 11 yearsJust create a
Menu
attribute in your activity and initialize it in theonCreateOptionsMenu
. -
Mediha almost 11 yearsI found the way how to call invalidateOptionsMenu, but basically it was this. Thank you.
-
Mediha almost 11 yearsThis is exactly what I did. Thank you.
-
mtmurdock over 9 yearsHow could I do this from my Fragment/Activity super class? I have a side navigation menu which swaps out the content fragment in my activity. When the side menu is opened, I want to disable all of the actionbar buttons. Using the above method I would have to do this in every content fragment class. Is there a way to do it from a parent fragment class more generically?
-
RedDeath over 2 yearsYou would now call
requireActivity().invalidateOptionsMenu()
if you are in a fragment