This is my menu in my Android app:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_fruit"
android:title="Apple" />
</menu>
I want to change the text of the item "Apple" to "Orange" so I put this in my onPrepareOptionsMenu
method in my activity:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem fruit = menu.findItem(R.id.menu_fruit);
fruit.setTitle("Orange");
return true;
}
This does not work. I also tried setting it from an outside method after the menu was declared in onCreateOptionsMenu
:
public Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_menu, menu);
this.menu = menu;
return true;
}
public void changeFruitTitle(){
MenuItem fruit = menu.findItem(R.id.menu_fruit);
fruit.setTitle("Orange");
}
None of these methods are working, the menu item on my screen still says "Apple" no matter what. I even put a breakpoint in the code and it says the value of the MenuItem
's title is "Orange" but on the screen it still reads "Apple".
Am I not refreshing the screen correctly? I call these functions again with
invalidateOptionsMenu();
But still it does not change the title.
Anyone know what could be the cause of this?