CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 219
Next let’s take a look at the onCreateOptionsMenu( ) method and see how it inflates your Menu
object using the Android MenuInflater class in conjunction with your /res/menu/main.xml XML
menu definition file.
Inflating the Menu: onCreateOptionsMenu( )
Let’s take a look at the Java code collapsing and expansion (plus + and minus –) icons on the left
side of the IDE central editing pane. Click on the plus + sign, next to the import statements, to
expand that section. This will turn the + next to the import statements section into a minus sign, as
shown in Figure 7-5 at the top. This will allow you can see all of the import statements, including the
highlighted import android.view.Menu statement, which Eclipse just provided (coded) for you.
Figure 7-5. The getMenuInflater( ) method chaining to a .inflate( ) method referencing the main.xml menu
Use the minus – sign next to each of your MainActivity class methods, except for the
onCreateOptionsMenu( ) method, for which you will click the plus + sign to open it up for
observation, so we can go over what it does. The result of this is shown in Figure 7-5 and you will
have four method names with plus + signs next to them! This is a handy way to “put away” your
Java method code blocks when you are not working on them, so that you can focus on the code
that you are currently writing, or in this case, the code in Figure 7-5 we are analyzing for learning
purposes.
The onCreateOptionsMenu(Menu menu) method uses a Menu object named menu, which it
passes over to the .inflate( ) method in the MenuInflater class along with the R.menu.main reference
to the XML menu definition. The .inflate( ) method call is referenced inside the MenuInflater class
using the Activity class getMenuInflater( ) method call, using a Java dot-chaining construct, like