Advertisement
English transcript
Let's look at how nested ViewGroups would apply to our app.
The information in our app looks like it's arranged in a vertical column.
So we can use a vertical LinearLayout for this.
This looks good so
far because we have one view per row in the vertical LinearLayout.
But when we get to this row, it's actually made up of multiple views.
We can use a horizontal LinearLayout to build out this row composed of a button,
a TextView, and a button.
So this LinearLayout would be the second child in
the bigger vertical LinearLayout.
In a moment, I'm going to show you three different XML layout files, and
I want to ask you which one you think accurately builds up
the layout that we want.
The catch is I don't want you to copy these files and
run them in Android Studio.
I want you to make an educated guess based off of reading and
understanding this code.
You can find this link in instructor notes.
It has three options listed, Option A, Option B, and Option C.
One of these options correctly builds out the layout that we want.
This is going to be challenging because we haven't previously encountered
XML with nested ViewGroups.
We're going to split this challenge across two quizzes, so
I won't ask you to make a decision yet.
For this first quiz, I want you to sketch out the view hierarchy diagram
for each of the XML files.
Here's a quick spiel on what view hierarchy means.
You'll need paper and
something to write with in order to sketch out these diagrams.
The way that Android organizes all these views on screen is in a view hierarchy,
or a tree structure of views.
This is simply a diagram that represents the view hierarchy.
It shows the parent and child relationships among the views.
The root view of this whole layout is a LinearLayout.
It has three children, a RelativeLayout, a TextView, and another TextView.
And then this RelativeLayout in turn has its own children, an ImageView and
a TextView.
For our Puddles the Pig app,
this is the view hierarchy that gets created by Android.
We have a RelativeLayout as a root view, and it has four children, an ImageView,
a couple TextViews, as well as a LinearLayout.
And this LinearLayout, in turn, has two ImageViews as its children.
Now you understand what a view hierarchy diagram is.
By sketching out the diagram for each of these XML layouts,
you'll better understand which view is the parent view, who the children are,
and whether those children have children views or not.
If you still need a hint, here are some examples of how to go from XML layouts
to drawing a view hierarchy diagram.
In this layout, we have a root view of a LinearLayout.
By the way, I added these
dots here because I couldn't fit all the code here.
With a root view as a LinearLayout, you draw that at the top of the tree.
And then it contains two children TextViews, so
you draw those as children underneath that, and that's it.
This is a more complicated layout.
We have a root view that's a LinearLayout, and
it contains three direct children, two TextViews, and a RelativeLayout, and
then inside that RelativeLayout, we have an ImageView and a TextView as children.
When you're done drawing out the view hierarchy diagram for
each option, then go ahead and check these boxes to continue.
In the following quiz, I'll ask you to make a selection
on which option is best, but for now, you don't have to make a decision.
The information in our app looks like it's arranged in a vertical column.
So we can use a vertical LinearLayout for this.
This looks good so
far because we have one view per row in the vertical LinearLayout.
But when we get to this row, it's actually made up of multiple views.
We can use a horizontal LinearLayout to build out this row composed of a button,
a TextView, and a button.
So this LinearLayout would be the second child in
the bigger vertical LinearLayout.
In a moment, I'm going to show you three different XML layout files, and
I want to ask you which one you think accurately builds up
the layout that we want.
The catch is I don't want you to copy these files and
run them in Android Studio.
I want you to make an educated guess based off of reading and
understanding this code.
You can find this link in instructor notes.
It has three options listed, Option A, Option B, and Option C.
One of these options correctly builds out the layout that we want.
This is going to be challenging because we haven't previously encountered
XML with nested ViewGroups.
We're going to split this challenge across two quizzes, so
I won't ask you to make a decision yet.
For this first quiz, I want you to sketch out the view hierarchy diagram
for each of the XML files.
Here's a quick spiel on what view hierarchy means.
You'll need paper and
something to write with in order to sketch out these diagrams.
The way that Android organizes all these views on screen is in a view hierarchy,
or a tree structure of views.
This is simply a diagram that represents the view hierarchy.
It shows the parent and child relationships among the views.
The root view of this whole layout is a LinearLayout.
It has three children, a RelativeLayout, a TextView, and another TextView.
And then this RelativeLayout in turn has its own children, an ImageView and
a TextView.
For our Puddles the Pig app,
this is the view hierarchy that gets created by Android.
We have a RelativeLayout as a root view, and it has four children, an ImageView,
a couple TextViews, as well as a LinearLayout.
And this LinearLayout, in turn, has two ImageViews as its children.
Now you understand what a view hierarchy diagram is.
By sketching out the diagram for each of these XML layouts,
you'll better understand which view is the parent view, who the children are,
and whether those children have children views or not.
If you still need a hint, here are some examples of how to go from XML layouts
to drawing a view hierarchy diagram.
In this layout, we have a root view of a LinearLayout.
By the way, I added these
dots here because I couldn't fit all the code here.
With a root view as a LinearLayout, you draw that at the top of the tree.
And then it contains two children TextViews, so
you draw those as children underneath that, and that's it.
This is a more complicated layout.
We have a root view that's a LinearLayout, and
it contains three direct children, two TextViews, and a RelativeLayout, and
then inside that RelativeLayout, we have an ImageView and a TextView as children.
When you're done drawing out the view hierarchy diagram for
each option, then go ahead and check these boxes to continue.
In the following quiz, I'll ask you to make a selection
on which option is best, but for now, you don't have to make a decision.