I have an action bar with a menuitem. How can I hide/show that menu item?
This is what I’m trying to do:
MenuItem item = (MenuItem) findViewById(R.id.addAction);
item.setVisible(false);
this.invalidateOptionsMenu();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Get a
MenuItempointing to such item, callsetVisibleon it to adjust its visibility and then callinvalidateOptionsMenu()on your activity so the ActionBar menu is adjusted accordingly.Update: A
MenuItemis not a regular view that’s part of your layout. Its something special, completely different. Your code returnsnullforitemand that’s causing the crash. What you need instead is to do:Here is the sequence in which you should call:
first call
invalidateOptionsMenu()and then insideonCreateOptionsMenu(Menu)obtain a reference to the MenuItem (by callingmenu.findItem()) and callsetVisible()on it