How to programmatically change an item in the options menu?

When the user presses the "Menu" button, I present several menu items. One of them is the exit button. I want to be able to change the text between "Sign In" and "Sign Out" depending on the boolean value that I am tracking. How can I programmatically change the text in the menu?

+1
source share
2 answers

You want to override the onPrepareOptionsMenu function in Activity. The menu parameter is passed, you can just find the menu item that you want to show / hide (or change the image and text) and return true.

onPrepareOptionsMenu

+3
source

Cancel / call onPrepareOptionsMenu(...) callback. Change what you add, depending on the input status.

As an alternative, I have not tested this, but, in my opinion, it works: when creating menu items, store the handle of the input / output element in a class variable and simply update it by calling setTitle(...) if necessary.

+3
source

All Articles