Advertisement
English transcript
In the last quiz we were trying to figure out how to give the Tom, Tim, and
Tod text views a little more personal space
by spacing them out equally vertically across the height of the device.
We tried setting a fixed dp value, but
that wouldn't work if we switch to a taller device like a tablet.
Then we tried match_parent, but then that made the Tom text view really big.
But then it knocked the other two textees off the screen.
So then we started researching about linear layout weights.
In the documentation, it said that for a vertical linear layout,
if we want to equally space out the children, we need to set
the height to be zero dp and a weight value of one for each of the children.
Assigning a weight value to a child view
is like assigning an importance value to it.
The higher the weight value,
the more space it may get if there is available space in the parent left.
For example, for
this vertical linear layout, it fills down vertically this way.
It turns out that this linear layout has a lot of extra space, and
that will be distributed to the children that have weight values.
If a child has a very high weight value,
then more of this available height will be given to that child.
If a child has a very tiny weight value,
then less of this available height will be given to that child.
I can show you what it looks like.
I'm going to set the height of this child to be 0dp and
then give it a weight of 1.
The linear layout first lays out Tim and Todd TextViews,
because they have a fixed height based on the content inside of it.
And then there's this Tom TextView that has an initial height of 0dp, but
a weight of 1.
None of the other children have a weight value,
because the default weight value is zero if it's not specified.
So this is the only child with a weight value.
So the parent gives all of the remaining height of the screen
to this Tom TextView.
And that's why you see the Tom TextView is so giant.
Let me try assigning weight values to the other children now.
So now each child has a height of 0dp and weight of 1.
This is the process that the linear layout would go through.
It would try to figure out the height of each of these views.
The height of these views starts at 0 dp.
So that means the available remaining height in the parent
is basically the whole height of the screen.
It divides the whole height of the screen by the weight sum,
which is three.
And then it gives one-third of that available height to Tom text view,
one-third of the height to the Tim text view,
and one-third to the Todd text view.
These will still be evenly spaced across the height of a device
even on larger screen devices as well because of this weight value.
If I change the weight value to be something higher,
then the parent will give more available height to the Tim TextView.
I can change it to an even higher weight value,
and you can see how it continues to get more available height.
In this case, the Tim TextView has a weight of 8.
Tom has a weight of 1, and Todd has a weight of 1.
So the weight subtotal is eight, nine, ten.
So the parent divides the available height divided by ten.
And one-tenth goes to Tom, eight-tenths goes to to Tim,
and one-tenth goes to Todd.
So for a vertical linear layout, say we have 3 child TextViews.
We set the initial height to be 0 dp.
So they're all stacked up at the top of the screen with 0 height.
That means the parent has all this extra height available.
If I set a weight of 1 on each child TextView, then the available
parent height will be distributed equally across each of the children.
The same would work for horizontal linear layout.
Say we have 3 child text views.
We start with a width of 0 dp.
That means the parent has all this extra width available.
If I assign a weight value of 1 to each child view,
then we're going to take the available width left in the parent and
distribute it equally among each child.
And again, it doesn't have to have a weight of 1.
It could be to three or any other value.
Or it could even have a weight of zero.
If a view has a weight of zero, that means that it won't change depending on
the available space left in the parent.
It will basically stay the width and the height that you give it.
One thing I want to point out is when to set the height to be 0 or
the width to be 0.
If you have a vertical linear layout,
you're filling the linear layout vertically this way.
So you don't know what the height should be, so
you set the height to be initially 0 dp, and you give it a weight value.
Similarly for horizontal linear layout, you're filling it horizontally.
So you don't know width yet of the child views.
So you set the width to be 0 dp, and give it a weight value.
Essentially, you can think of assigning a weight value to a view
as being able to stretch it out depending on how big the parent view is.
This is good for filling in the available space within a small device or
a large device.
Here are a couple examples in real life where layout weights are really useful.
This is the Google Hangouts app.
You can use it to chat with your friends or send sleepy penguins to them.
In this bottom part of the screen here is where you send your message.
Let's look at this layout down here a little more closely.
It's a horizontal linear layout because it's laid out in a horizontal row.
It's made up of an ImageView,
an EditText field where you can enter text, and another ImageView.
Now the height of each of these views is actually wrap_content
because it wraps through the content inside of it.
The width of these two image views is wrap_content because it's
only as wide as the content inside of it.
We give it a weight of 0 because we don't want it to be
any bigger than this, even if there's lots of space in the parent left over.
Setting the weight of 0 ensures that it will only be wrap_content for width and
height, and it won't change.
We want it to stretch so that it's a little bit bigger on the larger screen
devices, and we want it to shrink if it's on smaller screen devices.
So we set the width to be 0 dp and the weight to be 1.
That means that the parent linear layout will try the layout this image first and
then this image.
And then any remaining width in the device will be given to this view.
Here's an example in the maps app.
This is a detail page for a location.
There are three buttons here, Call, Save, and Website.
Let's look at them more closely.
You can actually build these as TextViews with icons attached to them,
and you can see the instructor notes for more details.
But that's really a tangent point.
For this exercise, let's assume that these are text views.
The height of each TextView is going to be wrap_content
because it wraps to the height of the content inside of it.
Now, the width is a little bit trickier.
We want each of these TextViews to be spread out equally across the width of
this screen, depending on whether it's a small device or a really large device.
We don't want all three text views to be grouped into
the left-hand side of the screen.
So to space children out equally, we give them equal weights.
We set a width of 0 and a weight of 1 for each of them.
That way the available width in the parent will be spread out equally across
the three children.
And the last example is an email app.
This is a compose screen in Gmail where you can write a new message.
It's a vertical linear layout, because the items are in a column.
And it's made up of four different EditText fields.
Now, the width of each child is match_parent because we want it to
stretch across the whole width of the device.
For height, we set these three edit text fields to be wrap_content so
it wraps the text inside of them.
We set the weights to be 0 because we don't want them
to increase in size even if there's available height in the parent.
But for this last EditText field, this is where you write your message, and we
want to give the user lots of space to write a really long letter to Grandma.
So we se the height to be 0 dp, and we give it a weight of 1.
So if the parent has any available height left,
it will be given to this child EditText field.
On a larger screen device, the compose email field will be a little bit taller,
and on a smaller device it will be a little bit smaller.
Now it's your turn to practice.
We want you to go ahead and build this layout in the XML visualizer.
You can start with the XML that we've provided below.
Here are a couple of tips.
When you try to tackle something like this, try to first determine the width
of each view, and then go ahead and try to determine the height of each view.
Also make sure that the image takes up any remaining height in
the parent linear layout.
So on a smaller screen device, it will take up less height.
On a larger screen device, it will take up more height.
Okay, go for it.
Check this box when you're done.
Tod text views a little more personal space
by spacing them out equally vertically across the height of the device.
We tried setting a fixed dp value, but
that wouldn't work if we switch to a taller device like a tablet.
Then we tried match_parent, but then that made the Tom text view really big.
But then it knocked the other two textees off the screen.
So then we started researching about linear layout weights.
In the documentation, it said that for a vertical linear layout,
if we want to equally space out the children, we need to set
the height to be zero dp and a weight value of one for each of the children.
Assigning a weight value to a child view
is like assigning an importance value to it.
The higher the weight value,
the more space it may get if there is available space in the parent left.
For example, for
this vertical linear layout, it fills down vertically this way.
It turns out that this linear layout has a lot of extra space, and
that will be distributed to the children that have weight values.
If a child has a very high weight value,
then more of this available height will be given to that child.
If a child has a very tiny weight value,
then less of this available height will be given to that child.
I can show you what it looks like.
I'm going to set the height of this child to be 0dp and
then give it a weight of 1.
The linear layout first lays out Tim and Todd TextViews,
because they have a fixed height based on the content inside of it.
And then there's this Tom TextView that has an initial height of 0dp, but
a weight of 1.
None of the other children have a weight value,
because the default weight value is zero if it's not specified.
So this is the only child with a weight value.
So the parent gives all of the remaining height of the screen
to this Tom TextView.
And that's why you see the Tom TextView is so giant.
Let me try assigning weight values to the other children now.
So now each child has a height of 0dp and weight of 1.
This is the process that the linear layout would go through.
It would try to figure out the height of each of these views.
The height of these views starts at 0 dp.
So that means the available remaining height in the parent
is basically the whole height of the screen.
It divides the whole height of the screen by the weight sum,
which is three.
And then it gives one-third of that available height to Tom text view,
one-third of the height to the Tim text view,
and one-third to the Todd text view.
These will still be evenly spaced across the height of a device
even on larger screen devices as well because of this weight value.
If I change the weight value to be something higher,
then the parent will give more available height to the Tim TextView.
I can change it to an even higher weight value,
and you can see how it continues to get more available height.
In this case, the Tim TextView has a weight of 8.
Tom has a weight of 1, and Todd has a weight of 1.
So the weight subtotal is eight, nine, ten.
So the parent divides the available height divided by ten.
And one-tenth goes to Tom, eight-tenths goes to to Tim,
and one-tenth goes to Todd.
So for a vertical linear layout, say we have 3 child TextViews.
We set the initial height to be 0 dp.
So they're all stacked up at the top of the screen with 0 height.
That means the parent has all this extra height available.
If I set a weight of 1 on each child TextView, then the available
parent height will be distributed equally across each of the children.
The same would work for horizontal linear layout.
Say we have 3 child text views.
We start with a width of 0 dp.
That means the parent has all this extra width available.
If I assign a weight value of 1 to each child view,
then we're going to take the available width left in the parent and
distribute it equally among each child.
And again, it doesn't have to have a weight of 1.
It could be to three or any other value.
Or it could even have a weight of zero.
If a view has a weight of zero, that means that it won't change depending on
the available space left in the parent.
It will basically stay the width and the height that you give it.
One thing I want to point out is when to set the height to be 0 or
the width to be 0.
If you have a vertical linear layout,
you're filling the linear layout vertically this way.
So you don't know what the height should be, so
you set the height to be initially 0 dp, and you give it a weight value.
Similarly for horizontal linear layout, you're filling it horizontally.
So you don't know width yet of the child views.
So you set the width to be 0 dp, and give it a weight value.
Essentially, you can think of assigning a weight value to a view
as being able to stretch it out depending on how big the parent view is.
This is good for filling in the available space within a small device or
a large device.
Here are a couple examples in real life where layout weights are really useful.
This is the Google Hangouts app.
You can use it to chat with your friends or send sleepy penguins to them.
In this bottom part of the screen here is where you send your message.
Let's look at this layout down here a little more closely.
It's a horizontal linear layout because it's laid out in a horizontal row.
It's made up of an ImageView,
an EditText field where you can enter text, and another ImageView.
Now the height of each of these views is actually wrap_content
because it wraps through the content inside of it.
The width of these two image views is wrap_content because it's
only as wide as the content inside of it.
We give it a weight of 0 because we don't want it to be
any bigger than this, even if there's lots of space in the parent left over.
Setting the weight of 0 ensures that it will only be wrap_content for width and
height, and it won't change.
We want it to stretch so that it's a little bit bigger on the larger screen
devices, and we want it to shrink if it's on smaller screen devices.
So we set the width to be 0 dp and the weight to be 1.
That means that the parent linear layout will try the layout this image first and
then this image.
And then any remaining width in the device will be given to this view.
Here's an example in the maps app.
This is a detail page for a location.
There are three buttons here, Call, Save, and Website.
Let's look at them more closely.
You can actually build these as TextViews with icons attached to them,
and you can see the instructor notes for more details.
But that's really a tangent point.
For this exercise, let's assume that these are text views.
The height of each TextView is going to be wrap_content
because it wraps to the height of the content inside of it.
Now, the width is a little bit trickier.
We want each of these TextViews to be spread out equally across the width of
this screen, depending on whether it's a small device or a really large device.
We don't want all three text views to be grouped into
the left-hand side of the screen.
So to space children out equally, we give them equal weights.
We set a width of 0 and a weight of 1 for each of them.
That way the available width in the parent will be spread out equally across
the three children.
And the last example is an email app.
This is a compose screen in Gmail where you can write a new message.
It's a vertical linear layout, because the items are in a column.
And it's made up of four different EditText fields.
Now, the width of each child is match_parent because we want it to
stretch across the whole width of the device.
For height, we set these three edit text fields to be wrap_content so
it wraps the text inside of them.
We set the weights to be 0 because we don't want them
to increase in size even if there's available height in the parent.
But for this last EditText field, this is where you write your message, and we
want to give the user lots of space to write a really long letter to Grandma.
So we se the height to be 0 dp, and we give it a weight of 1.
So if the parent has any available height left,
it will be given to this child EditText field.
On a larger screen device, the compose email field will be a little bit taller,
and on a smaller device it will be a little bit smaller.
Now it's your turn to practice.
We want you to go ahead and build this layout in the XML visualizer.
You can start with the XML that we've provided below.
Here are a couple of tips.
When you try to tackle something like this, try to first determine the width
of each view, and then go ahead and try to determine the height of each view.
Also make sure that the image takes up any remaining height in
the parent linear layout.
So on a smaller screen device, it will take up less height.
On a larger screen device, it will take up more height.
Okay, go for it.
Check this box when you're done.