Welcome to Part 3 of this review of the Pluralsight course Android Layout Fundamentals by Chiu-Ki Chan.
Chiu-Ki is a mobile developer with a passion in speaking and teaching.
Android Layout Fundamentals is the 5th course in the Android Learning Path.
Also in this series:
Chiu-Ki says a lot of people are intimidated by relative layout, because there’s a huge amount of attributes associated with it.
However, we just need to understand that it allows us to relatively position (or align) our views to parent or sibling views.
We begin with this:
We add a Button, and its default position is the top left of the screen, overlapping the EditText view.
To align it to the right we use:
When we add a second button it looks strange because the vertical position is wrong. We fix this with:
Chiu-Ki also shows us the other attributes we can use to position relative to sibling:
Finally we see the 7 attributes for positioning relative to a parent.
Chiu-Ki shows us a “center” view with four views around it: North, East, South, West.
This is what we create in this lesson, and we begin with just the center view inside a RelativeLayout:
With the attribute
layout_centerInParent set to true, this view displays in the middle of the relative layout.
By adding the attributes
android:layout_alignLeftwe position the north view.
We position we East view using
For the South view we use
Finally, to position the West view we use
We also learn about the attribute
wheareas layout_alignBottom lines up to the bottom of a view, this aligns to the bottom of the text.
Chiu-Ki asks what happens if some of our views are missing?
To explore this, we start by making the OK button invisible:
The position of the Cancel button remains unchanged. But if we change it to gone:
The cancel button is moved to the left hand side of the screen. This is the default position and the cancel button goes here because it cannot be to the left of a view that is gone.
There is an attribute called
This can be set to “true” and if so, the view becomes relative to the parent.