I have my menuItem on my res/menu/student_marks.xml file:
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));
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);
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);