October 2018
Intermediate to advanced
464 pages
15h 17m
English
We created an override for onCreateOptionsMenu(), just like we did in the previous recipe, Creating an options menu. But instead of inflating an existing menu resource, we created the menu using the Menu.add() method. Since we want to modify the menu items later as well as respond to the menu item events, we defined our own menu IDs and passed them to the add() method. The onOptionsItemSelected() object is called for all the menu items, so we get the menu ID and use a switch statement based on the ID. We return true if we are handling the menu event, otherwise we pass the event to the super class.
Changing the menu occurs in the onPrepareOptionsMenu() method. To simulate an external event, we created a button to toggle a Boolean ...