A (175)

(Tuis.) #1
CHAPTER 4: Introduction to XML: Defining an Android App, Its Design, and Constants 129

XML Inflation: How XML Works with Java


Since we are going to get into Java objects, as well as classes and methods and the like, in the next
chapter, I am going to briefly touch upon how all of this parameter-rich XML markup gets turned into
Java-compatible data structures.


These XML definition structures that contain your parent and child tags as well as any parameters
need to get transformed from XML-compatible data structures into Java-compatible data structures.
This is accomplished by the Android OS using a process called “inflation,” which you invoke using
the .inflate( ) or findViewById() method in your Java programming logic.


The Java-compatible data structures that are created from your XML definitions via this inflation
process are known as “objects,” and you will learn all about what Java objects are, and what they
are utilized for, in the next chapter, which covers the Java programming language.


Think of an empty Java object structure as a limp balloon, and your XML definition as the air that
will inflate it! What Android does, more accurately, is to take your XML definition and create a Java
object of the appropriate type using your definition; that is, by using the XML tags and parameters
you defined in your XML file.


For instance, for a

XML definition, the Android “inflater” will create a Menu Java object by
using the Android Menu class constructor, and will then populate this Menu object with MenuItem
sub-objects by using the Android MenuItem class. You will learn what a Java constructor is in the
next chapter, so hold on tight!


These MenuItem sub-objects, which are contained inside of a Menu “parent” object, will have
the configuration parameters for each of your Menu items (entries) set in exactly the way that you
defined them within your XML menu definition. This is accomplished by using data fields within the
MenuItem object that define each menu item in your menu using parameters (Android calls them
“properties”), which you defined inside of each tag in your XML menu definition.


Currently, this menu definition is contained in the main.xml file in the /res/menu folder of your
HelloUniverse Android application project hierarchy or folder structure. Before we finish the chapter,
let’s add a level of detail into our How Android Works graphic that adds in what we have learned
since we started Chapter 3.


XML’s Role: Revisiting How Android Works


Now that you have learned a bit more about XML, let’s revise Figure 3-1 from Chapter 3 and add in
some of the things we have learned about XML. I also added the Dalvik Virtual Machine (VM) and the
Android RunTime VM to the diagram just to be sure that I had everything covered, going from one
layer to the next. The updated graphic is shown in Figure 4-22.

Free download pdf