How to programatically change an item in the options menu?
Asked Answered
S

2

1

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

Siren answered 3/5, 2011 at 21:45 Comment(1)
in "onCreateOptionsMenu" method do "this._menu = menu;" in ur own method do "MenuItem item = _menu .findItem(1); item.setTitle("test");"Stevenson
F
3

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

onPrepareOptionsMenu

Foreshank answered 3/5, 2011 at 21:55 Comment(1)
Perfect--so much better than changing the item during onOptionsItemSelected()!Harrow
A
3

Override/Implement the onPrepareOptionsMenu(...) callback. Change what you add based on your sign-in state.

Alternatively, I haven't tested this but in my mind it works: When you create your menu items, keep a handle to the Sign In/Sign Out item in a class variable, and just update it by calling setTitle(...) as needed.

Anchie answered 3/5, 2011 at 21:54 Comment(0)
F
3

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

onPrepareOptionsMenu

Foreshank answered 3/5, 2011 at 21:55 Comment(1)
Perfect--so much better than changing the item during onOptionsItemSelected()!Harrow

© 2022 - 2024 — McMap. All rights reserved.