A (175)

(Tuis.) #1

220 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus


this: getMenuInflater( ).inflate(R.menu.main, menu). Once this .inflate( ) method has been called
successfully, the onCreateOptionsMenu( ) method will then return a true value, signifying the
completion of the Menu object inflation process.


Now that your Menu object is inflated using the bootstrap method that Eclipse created for you back
in Chapter 3, and which we just delved into the inner workings of how it stitches Android’s Menu
and MenuInflater classes together, we are going to finish implementing the Menu object and its
MenuItem sub-objects via the following objectives:


   Learn how to declare multiple TextView objects in a single Java declaration by
using commas.
 Create a public boolean onOptionsItemSelected( ) method to evaluate the
MenuItem objects.
 Learn how to create a Java switch programming structure, using case
statements to evaluate each MenuItem object.
 Instantiate an Intent object named editIntent using the Java new keyword, and
then utilize that Intent object in order to call the EditGalaxy.class, passing over
the current application Context object, by using the Java this keyword.
 Create the EditGalaxy.java Activity subclass, by using the Eclipse New Java Class
dialog, and its related Superclass Selection sub-dialog to define an
android.app.Activity superclass.
 Call a .startActivity( ) method from the Activity class and the android.app.Activity
package, passing over the editIntent Intent object, which contains the current
application Context and the binary representation of the EditGalaxy.class
Activity subclass which you wish to start.
 Create a New XML UI Layout Definition, by using the Eclipse New XML File
menu sequence.
 Create a RelativeLayout UI design, which contains both Button and EditText
UI widgets, using an XML UI definition file, and then inflate that UI design using
the setContentView( ) method call, inside of the onCreate( ) method, in the new
EditGalaxy.java Activity subclass.

The MenuItem Class: onOptionsItemSelected( )


In order to evaluate and process your MenuItem objects inside of your Menu object structure, you
will need to create a public boolean onOptionsItemSelected( ) method, which will be the next step
in your options menu implementation work process.


First, in order to make some additional room in the Eclipse central editing pane, let’s turn your seven
TextView object declaration and naming lines of code into just two lines of code, giving us back five
lines of code. To do this, I will show you a consolidation trick by which you can declare the TextView

Free download pdf