0

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?

Rob Bricheno
  • 4,467
  • 15
  • 29
K. Rawls
  • 95
  • 1
  • 3
  • 8
  • Are you calling invalidateOptionsMenu() before you change the value? – Davis Allen Feb 28 '18 at 22:51
  • No, I'm changing the value before I call invalidateOptionsMenu(); – K. Rawls Feb 28 '18 at 23:00
  • Have a look at that solution [enter link description here](https://stackoverflow.com/questions/7066657/android-how-to-dynamically-change-menu-item-text-outside-of-onoptionsitemssele) – D. B. Feb 28 '18 at 23:28

0 Answers0