A (175)

(Tuis.) #1
CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 215

The class hierarchy for the MenuInflater class is structured as follows, and this class was created
“from scratch” from the Java Object master class specifically to inflate Menu objects using XML
definition files stored in the /res/menu folder:


java.lang.Object



android.view.MenuInflater



It is interesting to note that you do not need to use an import android.view.MenuInflater;
statement at the top of your MainActivity.java class because there is a method which is part
of the android.app.Activity package called getMenuInflater( ) which provides access to the
MenuInflater class’s .inflate( ) method by using something called Java method “chaining” using the
getMenuInflater( ).inflate( ) Java code construct. As you will see later on in the code which we will
be writing in this chapter, the dot or period character chains the two Java methods together.


Designing Menus in XML Using

and


Your Menu object design is implemented using two primary XML definition files, specifically the
strings.xml file, located in the /res/values folder, which contains the MenuItem object’s label text
(String) constant, and the main.xml file, located in the /res/menu folder, which holds the Menu
object definition.


The first thing that you will need to do is to define your MenuItem object’s “title,” which I prefer to
call its “label,” in the strings.xml file. Right-click on this file in the /res/values folder, and use the
Open command to open it for editing in Eclipse. Create a child tag inside of the parent


tag, with the name edit_galaxy, and give it a text (String object) data value of “Edit
Galaxy Info” as is shown in Figure 7-1.

Figure 7-1. Add a constant named edit_galaxy with a text data value of Edit Galaxy Info for use in an options menu

Free download pdf