0

I'm following this turorial to add option menu to my app. I would hide one of the items when an action is performed. For example if the user does the login, then i want to hide the login item... How to select it for setting not visibile?

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.Login:
            clickLogin();   

        case R.id.BugReport:
            clickBugReport();

        case R.id.About:
            clickAbout();

        case R.id.Credits:
            clickCredits();

        default:
            return super.onOptionsItemSelected(item);
    }
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
smartmouse
  • 13,912
  • 34
  • 100
  • 166

1 Answers1

2

If you insist that you do not to show the menu item, when user has already logged in, then create another xml file with 'login' option removed. Before you set the menu xml file in onCreateOptionsMenu, check if user not logged in then set the old xml file else if logged in, then set the new xml file. That's a simple way.

Other way is to set the visibility of that item to be false. See setVisibility and How do I hide a menu item in the actionbar? and

If you just want to disable the login option, then you may check if user has logged in before performing the action on 'login' click, inside onOptionsItemSelected. Then you can show the background as disabled of that menu item.

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124