A (175)

(Tuis.) #1

106 CHAPTER 4: Introduction to XML: Defining an Android App, Its Design, and Constants


chapters covering Java programming and UI layout design. For now, we are just going to look at the
“syntax” of XML—that is, how it needs to be constructed or coded (written on the screen). We will
look at how it all works together in the next section of this chapter.


Since the tag does have a child tag nested inside of it, it uses a
different bracket configuration. At a high-level view, it will look like this:



Once you put parameters inside of the parent and child tags, and indent everything, so that you
know what level each of these tags and its parameters are supposed to be at, it will look exactly
like what you see in Figure 4-1, which I will replicate here. We will be going over what all of this XML
markup is doing during this chapter.


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />



In summary, any tag that is used as a parent tag will have an opening tag and
a closing tag
with the tag name in both the opening and the closing tag. The
closing tag will have a slash in front of the tag name to signify to the XML “parsing engine” (the code
that is interpreting the XML markup and turning it into something else; in this case, Java objects) that
this is a closing tag.


Alternatively, a child tag that has no children of its own will have the closing slash at the end of the
opening tag, like this: which allows a much more compact way
of writing a child tag. You might think of this as an implicit closing tag, so a tag with no child tags
nested inside of it will not have an explicit (backslash in front of tag name inside of < > chevron
bracketing) closing tag like the UI layout container has. Since a layout container is
inherently a container and will always have child tags, it will always have an explicit closing tag at the
bottom of the XML definition file.


It’s important to note that tag nesting can be more than one level deep, so you can have the
following structure:







Free download pdf