0

The regular way to change programmatically menu item icon is save reference to the menu after onCreateOptionsMenu() called:

private Menu mOptionsMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mOptionsMenu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

and then access it like: mOptionsMenu.findItem(R.id.action_something).setIcon(R.mipmap.new_icon);

My question is how to set new icon before onCreateOptionsMenu() called - so I don't have reference to the menu?

Thanks,

michael
  • 3,835
  • 14
  • 53
  • 90

1 Answers1

0

I don't think there is a way to get the reference to the menu before onCreateOptionsMenu(). Also why would you want to set an icon before the onCreateOptionsMenu?

Apologies for posting it as an answer as I need 50 points to be able to comment.

pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • It's complex, I have `GpsStatus.Listener` that according to gps status set icon on the menu. So there I need the reference... and it seems that the listener called before onCreateOptionsMenu – michael Sep 30 '15 at 21:14
  • you can always use invalidateOptionsMenu() whenever the status changes to draw the menu again and set the appropriate icon in the menu. – pgiitu Sep 30 '15 at 21:18
  • note that it's seems impossible to control when the location listener called first time in the lifecycle – michael Sep 30 '15 at 21:27
  • If it is slow because there are too many updates and it is not feasible to redraw the whole menu again and again and also since you cannot control the callback for the location listener, I would suggest you use a strip or something like that below action bar to show the status of the gps listener instead of using the actionbar menu. – pgiitu Sep 30 '15 at 21:35