Enable/Disable ActionBar Menu Item
Solution 1
@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);
}
Solution 2
I found this post because I wanted to achieve the same result. None of the other answers were completely helpful in getting this to work for me. After some research and trial and error I got mine to work. So I decided to share my results.
Variables I created for this task:
MenuItem save_btn;
boolean b = false;`
Then set up the Actionbar Menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.update_menu_item, menu);
save_btn = (MenuItem) menu.findItem(R.id.action_save);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
save_btn.setEnabled(b);
super.onPrepareOptionsMenu(menu);
return true;
}
Since the variable boolean b
is initialized as false the save_btn
is disabled when the activity is created.
Here is the method to toggle the save_btn
using @OpenSourceRulzz example:
private void updateSaveButton (CharSequence s){
String text = null;
if(s != null){
text = s.toString();
}
if(text != null && text.trim().length() != 0){
b = true;
}
else{
b = false;
}
}
This method is called through the TextWatcher()
function for the EditText
box in onCreate()
again using @OpenSourceRulzz example
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_project);
EditText projectName = (EditText) findViewById(R.id.editTextProjectName);
projectName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
updateSaveButton(s);
invalidateOptionsMenu();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after){}
@Override
public void afterTextChanged(Editable s) {}
});
}
The last piece of the puzzle was adding the invalidateOptionsMenu()
method.
The part that I came up with that made mine work was using the boolean b
variable to toggle the state of of the save_btn
.
Solution 3
After struggling for 1 hour after this stupid issue, the only solution that worked for me is:
store the Menu in a local variable:
Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Then change enabled status simply by calling:
menu.findItem(R.id.action_save).setEnabled(true);
And you can initially disable it just in the xml...
Solution 4
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (your condition) {
menu.findItem(R.id.saveButton).setEnabled(false);
} else {
menu.findItem(R;id.saveButton).setEnabled(true);
}
return super.onPrepareOptionsMenu(menu);
}
With this method, your menu item will be disabled each time your condition is checked, and will be enabled if not.
Related videos on Youtube
Coder
Updated on July 14, 2022Comments
-
Coder almost 2 years
I have actionbar menuitems cancel and save.
menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/saveButton" android:showAsAction="always" android:title="@string/save" android:visible="true"> </item> <item android:id="@+id/cancelButton" android:showAsAction="always" android:title="@string/cancel" android:visible="true"> </item> </menu>
I want to disable save menuitem when activity is started.
My activity code -
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.add_project); EditText projectName = (EditText) findViewById(R.id.editTextProjectName); projectName.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { configureSaveButton(s); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) {} @Override public void afterTextChanged(Editable s) {} }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.addprojectmenu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem item = (MenuItem) findViewById(R.id.saveButton); item.setEnabled(false); return super.onPrepareOptionsMenu(menu); } private void configureSaveButton(CharSequence s){ String text = null; if(s != null){ text = s.toString(); } if(text != null && text.trim().length() != 0){ }else{ } }
So what I am trying to do here is, initially when activity is started save menu item should be disabled and when editext contains some text then it should be enabled.
I am not sure what should be the code in if else in configureSaveButton method. Also how can i disable save menu item initially.
I get null pointer exception in onPrepareOptionsMenu.
I am using android 4.1
-
Prashant Gosai over 8 yearstry this one best way to disable menu item icon stackoverflow.com/a/33910273/4449159
-
-
dmon over 11 yearsYou also need to call
invalidateOptionsMenu()
when you want it to be updated. -
Coder over 11 yearsThanks for the answer how can i re enable it again? what should be the code in configureSaveButton method.
-
Coder over 11 yearsCan you please tell Where do we call invalidateOptionsMenu() if I use Pratik's code?
-
Pratik over 11 yearsYou can get the menu item and save it in MenuItem object and set the value for this as you want and last call the invalidateOptionsMenu() method to update the ActionBar menu
-
dmon over 11 yearsIn onTextChanged() just save if the value of s is empty somewhere and call invalidateOptionsMenu(). Then when inflating the menu check if the flag is true or false.
-
Coder over 11 yearsI just saved MenuItem in class variable and set the enable/disable accordingly. Thanks
-
Jabari almost 11 years@Pratik - Thanks, your tip saved me a few lines of coding!
-
android developer over 9 yearsIs it really recommended to disable action items instead of just hide them? as a user, I don't think i'd like to have action items just staying around that I can't click...
-
Eugen Martynov over 9 yearsI think it is not recommended. Even more in lollipop disabled item looks like enabled. So user can not distinguish between two of them
-
Shridutt Kothari almost 8 yearsYes @dmon you were right thanks, i was struggling due to lack of invalidateOptionsMenu(); my manu was not getting updated till i click on NavigationDrawer