YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 124 lines · added Nov 7, 2015

Report a problem
What's wrong?
Advertisement

English transcript

When positioning children relative to other sibling views,
you can add constraints on their position.
For example, one TextView must be above another TextView,
or one ImageView must be to the left of another TextView.
All these constraints remind me of making a seating chart for
a dinner party.
We do have our birthday party celebration coming up, so
let's look at the seating chart for that.
We invited all these people over for
dinner But there are constraints on where people want to sit.
Some people must sit next to their best friend.
Other people have what you might call frenemies.
So, they might not want to sit next to certain people.
So, taking into account all of that, I came up with this proposed layout.
Kunal, Ben, and Kagure are up here.
Natalie, Lyla, and me are down here And Amy, Omoju and Jenny are in this corner.
So could we build this seating arrangement in XML using TextViews and
a relative layout?
We know how to position views in corners.
So the Lyla TextView would be aligned to the parent's left edge and bottom edge.
The Jennie TextView would be aligned to the parent's right edge and bottom edge.
For Ben we would align them to the top edge of the parent.
By the way, we're assuming that the parent is the full width and
height of the screen, so we've set match parent for width and height.
So back to Ben.
He's aligned to the top of the parent.
We also want to center him horizontally.
And formerly, you might call these three views the anchor views.
Because everyone else is positioned relative to these people.
And these people won't move, regardless of where everyone else is sitting.
So let's add another person.
We have Kunal here, who's located to the left of Ben.
But how do we express that in XML?
Well, there's no specific attribute that says that a view should be to the left
of a view named Ben.
All the Android attribute names are general so
that it works for any use case that you'd want.
We want to position Kunal to the left of Ben, but
we don't know of any existing attribute names that can do that.
Luckily though,
RelativeLayout has an attribute that says android: layout_toLeftof.
This means that the Kunal text view should be to the left of some view.
And the way that we specify some view is to use view IDs.
We can assign view ID names to any view.
They don't have to be specifically addressed for RelativeLayout.
They could be located within a RelativeLayout, or a LinearLayout, or
any other view group.
So on the Ben TextView, we can add this attribute, android:id.
The value of it is the ID name.
The format starts with @ symbol, @ refers to a resource in our Android app.
We've also seen this @symbol for referring to images in our app.
For example app Drawable.
Then we specify the resource type.
In this case, the resource type is an id.
We also add the plus symbol because this is the first time we're
declaring this id.
Then we have a slash and then we have the name of the view.
This is the name that we chose for the Ben TextView.
We call it ben_text_view.
The basic requirements are that the name can't have any spaces.
That's why we use underscore symbols here.
It needs to start with a letter and
it can't contain any of the weird punctuation symbols.
Numbers are fine though.
So in this case we give Ben the ID of ben_text_view.
That way we can refer to it later.
So on the Kunal TextView we can add the attribute layout_toLeftof and
we can say it's to the left of the Ben TextView.
We refer to the ID for the Ben TextView using @id.
We don't need to use the plus symbol because We've already defined that ID.
This is the second time we're using the ID.
When you come up with view ID names, make sure that they're all unique names.
Usually, picking something related to the content inside of it is helpful.
Okay, cool, so that's how you position Kunal to the left of Ben.
Now, say, for example, we want to position Omoju above Jennie.
Since we're going to need to refer to Jennie,
we should create an ID for Jennie.
On the Jennie TextView, we specify this attribute and give it this value.
The ID name is jennie_text_view.
On the Omoju TextView, we add this attribute.
This says that Omoju should be positioned above the jennie_text_view.
And this is the ID that we created up here.
Let's look at the XML layout.
We have a relative layout where the width and height are match_parent.
Then we have the lyla_text_view.
It's aligned to the parent's bottom left corner, and
that's what we see down here.
Then we have the jennie_text_view.
It's aligned to the parents bottom right corner.
So that's over here.
Then we have the ben_text_view.
And it's aligned to the top edge of the parent.
It's also centered horizontally.
These are the anchor views and
now we can position the other views around them.
Notice that for each of the views I already went ahead and added an ID.
So this is the Ben TextView ID.
Then we can position the Kunal TextView to the left of Ben.
I align Kunal to the top of the parent, and to the left of the Ben TextView.
I use the view ID for Ben right here.
Then we position the Omoju TextView above Jenny.
Omoju is aligned to the right edge of the parent, and
above the Jenny TextView.
Okay cool.
So there's more friends to position correctly in this layout.
In the instructor notes below we've provided you a link
to the XML Visualizer with some initial code already in there.
Go ahead and modify the XML so
that we can achieve this desired layout for everyone.
Some of the views are already positioned incorrectly, so
you'll need to fix those.
By the way, we just introduced a ton of new attributes.
But don't feel like you have to memorize all of them.
You can always look them up at any time.
You can just search for a relativelayout, layout params.
If you click on this first link, you go to the documentation for this.
So these are all the XML attributes that you can put on a child view
within a RelativeLayout so that the RelativeLayout knows how to lay it out.
Here are some that you're already familiar with.
Layout_above, alignParentbottom, alignParentright and so on.
Note that for
teaching purposes, the XML Visualizer may not support all of these attributes.
However, when you're developing an app in Android Studio you
should be able to use all of these.

Need subtitles from another video?