How to rename an existing menu item?

Do you know how to rename an existing menu?

I can rename when I click a menu item. But I do not know how to access a menu item when a button is pressed.

Please advice.

+6
android
source share
3 answers

It would be nice if you could clarify this question a bit, but every time the user presses the Menu button on his Android device, while inside one of your actions, the onPrepareOptionsMenu method is onPrepareOptionsMenu . The first time the menu is displayed (i.e., only once), the onCreateOptionsMenu method is onCreateOptionsMenu .

Basically, the onPrepareOptionsMenu method is where you have to make any changes, such as enabling / disabling certain menu items or changing the text of a menu item as the case may be.

As an example:

 @Override public boolean onPrepareOptionsMenu(Menu menu) { // Check current message count boolean haveMessages = mMessageCount != 0; // Set 'delete' menu item state depending on count MenuItem deleteItem = menu.findItem(R.id.menu_delete); deleteItem.setTitle(haveMessages ? R.string.delete : R.string.no_messages); deleteItem.setEnabled(haveMessages); return super.onPrepareOptionsMenu(menu); } 
+27
source share

Use MenuItem.setTitle () . If this is not what you need, you should be more specific.

0
source share

OnPrepareOptionsMenu is the right place to make changes to menu items.

0
source share

All Articles