1

My menu has a item to Log in, but when you are logged in I want it to say Log out.

How?

If I'm going to change the item after its created, its probably through this method

@Override
public boolean onCreateOptionsMenu( Menu menu ) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.menu_main, menu );
    return true;
}

Afaik the onCreateOptionsMenu() happens after the onCreate so putting any getItemId() for the menu there will give me a NullPointerException right away.

I want the app to find out if its supposed to use the string R.string.Logout if its logged in.

I dont even know what to search for for this issue. All I found was how to make a string implement names, like this answer https://stackoverflow.com/a/7646689/3064486

2
  • please post some code if you expect a proper answer. Hard to tell what's being asked here Commented Jul 25, 2014 at 20:19
  • fair enough, updating Commented Jul 25, 2014 at 20:23

3 Answers 3

1

You should use onPrepareOptionsMenu(Menu menu) instead to update menu items

@Override
public boolean onPrepareOptionsMenu(Menu menu){
        super.onPrepareOptionsMenu(menu);


        MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
        someMenuItem.setTitle("Log out");
        return super.onPrepareOptionsMenu(menu);
    }

To refresh Menu items call invalidateOptionsMenu();from Activity

From Android API guides: "If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)"

Sign up to request clarification or add additional context in comments.

Comments

0

After you inflate a menu, you can customize its items. To get each one, you must call findItem() with the item's id. In particular, you can use setTitle() to change the displayed string.

For example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    if (mIsLoggedIn)
        menu.findItem(R.id.action_login).setTitle("Log out");

    return true;
}

where action_login is the id you set for this particular menu item in the menu's xml file.

Comments

0
 private void updateUI() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                Menu menu = navigationView.getMenu();
                MenuItem nav_signin = menu.findItem(R.id.nav_signin);
                nav_signin.setTitle(MyApp.signedIn ? "Sign out" : "Sign in");
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.