Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable ActionBar Menu Item

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

like image 243
Coder Avatar asked Jan 05 '13 05:01

Coder


People also ask

How do I hide a menu item in the Actionbar?

You can set a flag/condition. Call invalidateOptionsMenu() when you want to hide the option. This will call onCreateOptionsMenu() .

How do I disable menu items?

Disabling a MenuItem You can set the value to this property using the setVisible() method. To disable a particular menu item invoke the setVisible() method on its object by passing the boolean value “false”.

Which of the following method is used to enabling and disabling menu items?

Solution. To enable and disable menu items, use the MenuItem class's setEnabled method.


1 Answers

@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);  } 
like image 121
Pratik Avatar answered Sep 20 '22 16:09

Pratik