A (175)

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

As you can see, the closing tags must be in the reverse order from the opening tag order, so that the
XML tag structure exhibits proper nesting within its level hierarchy.


XML Referencing: Chain XML Constructs Together


XML files can also “reference” other XML files, so that you can create a “chain” where your XML
definitions can be more “modular.” XML file referencing is somewhat akin to XML tag nesting, but it
spans across files. Referencing in Android uses the @ symbol, which is specific to Android XML file
referencing.


If you were wondering what the @dimen/ and @string/ “prefixes” were in the activity_main.xml UI
layout XML markup shown in Figure 4-1, these are references to XML tags and parameters in the
dimens.xml and strings.xml XML definition files.


Android uses the @ character to signify that another XML file is being referenced, as you will see
over the rest of the chapter, as we look at how to use XML markup to define values, dimensions,
menus, styles, themes, and your Android Manifest, which controls everything about your application
and how it works.


The @ character allows XML files to reference other XML files, allowing developers to create their
own logical XML structures. This allows XML markup to be modular and allows developers to
organize and optimize their XML definitions using multiple files. Without this referencing capability,
XML markup would end up being all lumped together in one or two massive files. Referencing allows
this XML “structure” to be created, such as the XML structure for your HelloUniverse application,
which the Eclipse ADT put into place for you with the New Android Application Project series of
dialogs.


We’ll be looking at your application’s current XML structure, and all of the files within it, as well
as how these XML files reference each other. We will also be looking at how to change the tag
parameter values within these XML files in order to customize your Android application during the
remainder of this chapter.


Once we’re all finished looking at each of the XML files that are currently in your bootstrap project,
I’ll include a visual of how all of these go together to for the foundational XML structure for your
Android application that you will be building upon during the rest of this book. If you want to cheat,
and look ahead in this chapter, you can go ahead and take a quick peak, the visual is Figure 4-17.


Let’s take a closer look at the strings.xml file in the /res/values folder first, as this is one of the most
often used XML files in Android application development. Open the /res/values folder by clicking
the right-facing arrow next to it. Right-click on the strings.xml file and select the Open option from
the context-sensitive menu.


As you can see in Figure 4-2, the strings.xml file does not need to reference the XML repository URL,
as it just contains resource definitions, using the parent tag and, in this case, child


tags, defining each string value, and giving the value a (variable) name. We will get into
variables in the next chapter on Java. The reason this file does not require an xmlns:android XML
naming schema definition at the top of the XML definition is because the attributes or parameters
used inside of the tags in this file do not preface themselves with something else. Notice in Figure 4-2
you are using name=“app_name” and not android:name=“app_name” and realize that if you
were using android:name=“app_name” that you would then need an xmlns:android naming schema
definition in place before you used this parameter name convention.
Free download pdf