Advertisement
English transcript
Let's review.
So far we've learned that a linear layout can have an orientation that's
horizontal or vertical.
But now let's think about whether these views are taking full advantage of
the screen real estate.
When I say real estate, I mean the available space on the screen.
For our Nexus 6 phone,
say we have a vertical linear layout that has three child text views inside.
Tom, Tim and Todd.
When we hold the device in portrait mode like this in our hand,
then all the text views are grouped in the top left corner of the screen.
There's all this blank space left unused.
Similarly, if we rotated the device to Landscape mode,
then the text is on the top left corner and there's all this blank space left.
It's even more noticeable on a tablet device,
where the screen is a lot bigger.
We're only using a small amount of the screen real estate, and
the rest of it is blank.
We're going to learn about how we can more equally spread out these views
across the screen to take up more of the available room.
This applies to other views as well.
You can have bigger images across the screen for
a better experience, instead of having them really tiny in the top left corner.
Same with buttons,
you could have bigger buttons so that it's easier To tap on those buttons.
For the purposes of our example, we're going to stick with text views.
So let's go to the XML visualizer to figure out how we can spread out
these views.
So here's the code for Tom, Tim, and Todd.
I have three text views within a vertical linear layout and
this is what they look like on the device.
Now, I want to show you how you can spread out these three text views.
So that Tom is up here.
Tim is here.
And Todd is down here.
Clearly, setting height for wrap_content is not doing the job.
The other thing we learned is to set a fixed dp value for the height.
So I'm going to set the height to be, for example, 200dp.
Cool. So that left a lot more space for Tom.
If I do the same for the other TextViews,
then they also spread out more evenly across the screen.
But this solution will only work for the sides of a device.
If I have a tablet like I showed you earlier, then these views are still
going to be grouped in somewhat of the top left corner.
The other option for setting height is match parent which we learned earlier.
When you set match parent for the Tom TextView
it takes the height of the whole parent which is a whole screen.
That pushes the other text views off the screen so you can't even see them.
So let's not do that.
Well now we're out of ideas because we tried wrap content for
the height, which had a fixed DP value.
And we tried match parent.
And none of those give us the right spacing across all different devices.
Let's Google search for a potential solution to this problem.
We're using a linear layout view group.
And we want to equally space out the children views.
So let's search for that.
Okay, so there's a bunch of search results, but
let's click on this first one.
This is a link to the Stackoverflow site.
It's a Q&A site where you can post questions and
get answers from other Android developers.
I use it all the time when I'm stuck and I need help on a problem.
Often times other people will have already asked a question
that I'm having.
So this question here is about how to evenly distribute buttons
across the width of a linear layout.
This is similar to what we're looking for,
except instead of buttons we're working with text views.
And instead of the width of the linear layout we're trying to spread it across
the height of the linear layout.
We can read through the question or we can skim it.
Usually I just skim for the answer.
So this solution received 88 votes and has a green checkmark on it.
It talks about how you can set layout_width to 0dp and
set the layout_weight for each of the buttons to be 1.
And that splits the available width equally across the buttons.
You may not fully understand the answer at this point, but that's okay.
One key phrase that we saw is this linear layout_weight.
We haven't encountered this before, but it sounds important to the answer.
So lets copy it and Google search for
more information Information about layout_weight.
So let's search for linear layout layout_weight.
The first result is an article for linear layout and
it's on the developer.android.com site, so let's click on it.
So this is an article about the Linear Layout.
We already learned the stuff at the top about the Android orientation attribute,
but then there's new information about something called Layout Weight.
There's also an example at the bottom of the screen here with some code and
a screenshot.
I could summarize all of this for
you, but first I want to give you a chance to read this document.
And try to figure it out on your own.
This is going to be a lot more similar to how you're going to solve problems
after this course is over.
You'll look for some information online and
try to figure out how that can be used to solve your problem.
Okay, so this is the layout that we have now on the device.
And this is what we want to achieve.
The three texts are spread-out evenly across the height of the device.
To solve this problem,
I want you to read the Linear Layout article that we linked below.
It's perfectly normal for
you to not understand everything that's in the article.
Often times you just want to have some vague understanding
of how the concepts in the article may be able to help you in your work.
Then I want you to provide some ideas on how we can build this desired
layout in XML.
You don't need to provide the full XML layout code
because we're going to walk through that together in the next video.
But here you could write a couple sentences or
bullet points on how the information in the article could relate to our problem
So far we've learned that a linear layout can have an orientation that's
horizontal or vertical.
But now let's think about whether these views are taking full advantage of
the screen real estate.
When I say real estate, I mean the available space on the screen.
For our Nexus 6 phone,
say we have a vertical linear layout that has three child text views inside.
Tom, Tim and Todd.
When we hold the device in portrait mode like this in our hand,
then all the text views are grouped in the top left corner of the screen.
There's all this blank space left unused.
Similarly, if we rotated the device to Landscape mode,
then the text is on the top left corner and there's all this blank space left.
It's even more noticeable on a tablet device,
where the screen is a lot bigger.
We're only using a small amount of the screen real estate, and
the rest of it is blank.
We're going to learn about how we can more equally spread out these views
across the screen to take up more of the available room.
This applies to other views as well.
You can have bigger images across the screen for
a better experience, instead of having them really tiny in the top left corner.
Same with buttons,
you could have bigger buttons so that it's easier To tap on those buttons.
For the purposes of our example, we're going to stick with text views.
So let's go to the XML visualizer to figure out how we can spread out
these views.
So here's the code for Tom, Tim, and Todd.
I have three text views within a vertical linear layout and
this is what they look like on the device.
Now, I want to show you how you can spread out these three text views.
So that Tom is up here.
Tim is here.
And Todd is down here.
Clearly, setting height for wrap_content is not doing the job.
The other thing we learned is to set a fixed dp value for the height.
So I'm going to set the height to be, for example, 200dp.
Cool. So that left a lot more space for Tom.
If I do the same for the other TextViews,
then they also spread out more evenly across the screen.
But this solution will only work for the sides of a device.
If I have a tablet like I showed you earlier, then these views are still
going to be grouped in somewhat of the top left corner.
The other option for setting height is match parent which we learned earlier.
When you set match parent for the Tom TextView
it takes the height of the whole parent which is a whole screen.
That pushes the other text views off the screen so you can't even see them.
So let's not do that.
Well now we're out of ideas because we tried wrap content for
the height, which had a fixed DP value.
And we tried match parent.
And none of those give us the right spacing across all different devices.
Let's Google search for a potential solution to this problem.
We're using a linear layout view group.
And we want to equally space out the children views.
So let's search for that.
Okay, so there's a bunch of search results, but
let's click on this first one.
This is a link to the Stackoverflow site.
It's a Q&A site where you can post questions and
get answers from other Android developers.
I use it all the time when I'm stuck and I need help on a problem.
Often times other people will have already asked a question
that I'm having.
So this question here is about how to evenly distribute buttons
across the width of a linear layout.
This is similar to what we're looking for,
except instead of buttons we're working with text views.
And instead of the width of the linear layout we're trying to spread it across
the height of the linear layout.
We can read through the question or we can skim it.
Usually I just skim for the answer.
So this solution received 88 votes and has a green checkmark on it.
It talks about how you can set layout_width to 0dp and
set the layout_weight for each of the buttons to be 1.
And that splits the available width equally across the buttons.
You may not fully understand the answer at this point, but that's okay.
One key phrase that we saw is this linear layout_weight.
We haven't encountered this before, but it sounds important to the answer.
So lets copy it and Google search for
more information Information about layout_weight.
So let's search for linear layout layout_weight.
The first result is an article for linear layout and
it's on the developer.android.com site, so let's click on it.
So this is an article about the Linear Layout.
We already learned the stuff at the top about the Android orientation attribute,
but then there's new information about something called Layout Weight.
There's also an example at the bottom of the screen here with some code and
a screenshot.
I could summarize all of this for
you, but first I want to give you a chance to read this document.
And try to figure it out on your own.
This is going to be a lot more similar to how you're going to solve problems
after this course is over.
You'll look for some information online and
try to figure out how that can be used to solve your problem.
Okay, so this is the layout that we have now on the device.
And this is what we want to achieve.
The three texts are spread-out evenly across the height of the device.
To solve this problem,
I want you to read the Linear Layout article that we linked below.
It's perfectly normal for
you to not understand everything that's in the article.
Often times you just want to have some vague understanding
of how the concepts in the article may be able to help you in your work.
Then I want you to provide some ideas on how we can build this desired
layout in XML.
You don't need to provide the full XML layout code
because we're going to walk through that together in the next video.
But here you could write a couple sentences or
bullet points on how the information in the article could relate to our problem