23

I have my menuItem on my res/menu/student_marks.xml file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".StudentMarks" >
    <item android:id="@+id/action_selected_year"
        android:title="@string/action_selected_year"
        android:showAsAction="withText|ifRoom"
        style="@style/AppTheme" />
</menu>

Now i need for this item to set a title.in a specific part of my app.

I can work with a specific item in this method:

onOptionsItemSelected(MenuItem item)

the problem is that i need the item 'action_selected_year' without of this method but in another part of my program.
I don't have idea how to get it.

Giovanni Far
  • 1,623
  • 6
  • 23
  • 37

3 Answers3

45
Menu optionsMenu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.main, menu);
   //  store the menu to var when creating options menu
   optionsMenu = menu;
}

And to get a menu item:

MenuItem item = optionsMenu.findItem(R.id. action_selected_year);
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
Esteban Atenor
  • 551
  • 1
  • 4
  • 4
  • 4
    You can also retrieve the menu directly from your toolbar (if it's a toolbar from layout): Make sure your toolbar have an id in your layout and retrieve it thanks to KTX or to ViewBinding. Let's say we are using ViewBinding, it looks like: "viewBinding.my_toolbar_id.menu.findItem(id_of_my_menu_item)" No need to store any variable. – A.Mamode Jan 25 '21 at 18:20
  • view binding doesn't work with toolbar. – Ray Chakrit Aug 11 '22 at 20:07
11
Menu optionsMenu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       //  store the menu to var when creating options menu
       optionsMenu = menu;
    }

example: change icon on first menuItem (optionsMenu should be != null)

optionsMenu.getItem(0).setIcon(getResources()
    .getDrawable(R.drawable.ic_action_green));
Michael D.
  • 1,795
  • 2
  • 18
  • 25
  • 1
    Perfect, save the reference of the menu inside of the activity variable, allow to me a perfect control on the menu object. Thank you. – Giovanni Far Nov 12 '14 at 02:48
  • 12
    That does not get the menu item by ID. That simply returns the first menu item. – Johann Dec 01 '15 at 00:48
  • 9
    You can call findItem() and pass the id you're looking for, instead of calling getItem(0). – ali-hk Feb 18 '17 at 19:19
2

If your menu inside NavigationView example:

<com.google.android.material.navigation.NavigationView 
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu_layout" />

then you can find your menu item by id like this

NavigationView navigationView = findViewById(R.id.navigationView);
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.your_menu_item_id);
  • This appears to be a duplicate of another answer and is at risk of being down voted. To avoid that, either explain why it is different or delete it. – bartonstanley Feb 28 '20 at 17:59