gravity.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group==orientation) {
if (checkedId==R.id.horizontal) {
orientation.setOrientation(LinearLayout.HORIZONTAL);
} else {
orientation.setOrientation(LinearLayout.VERTICAL);
}
} else if (group==gravity) {
if (checkedId==R.id.left) {
gravity.setGravity(Gravity.LEFT);
} else if (checkedId==R.id.center) {
gravity.setGravity(Gravity.CENTER_HORIZONTAL);
} else if (checkedId==R.id.right) {
gravity.setGravity(Gravity.RIGHT);
}
}
}
}
In onCreate()
, we look up our two RadioGroup
containers and register a listener on each, so we are notified when the radio buttons change state (setOnCheckedChangeListener(this)
). Since the activity implements OnCheckedChangeListener
, the activity itself is the listener.
In onCheckedChanged()
(the callback for the listener), we see which RadioGroup
had a state change. If it was the orientation group, we adjust the orientation based on the user’s selection. If it was the gravity group, we adjust the gravity based on the user’s selection.
Figure 7-2 shows the result when the sample application is first launched inside the emulator.
Figure 7-2. The LinearLayoutDemo sample application, as initially launched
If we toggle on the Vertical radio button, the top RadioGroup
adjusts to match (see Figure 7-3).
Figure 7-3. The same application, with the Vertical radio button selected
If we toggle the Center or Right radio button, the bottom RadioGroup
adjusts to match (see Figures 7-4 and 7-5).
Figure 7-4. The same application, with the Vertical and Center radio buttons selected
Figure 7-5. The same application, with the Vertical and Right radio buttons selected
All Things Are Relative
RelativeLayout
, as the name suggests, lays out widgets based upon their relationship to other widgets in the container and in the parent container. You can place Widget X below and to the left of Widget Y, or have Widget Z’s bottom edge align with the bottom of the container, and so on.
This is reminiscent of James Elliott’s RelativeLayout
[13] for use with Java Swing.
Concepts and Properties
To make all this work, we need ways to reference other widgets within an XML layout file, plus ways to indicate the relative positions of those widgets.
The easiest relations to set up tie a widget’s position to that of its container:
• android:layout_alignParentTop
says the widget’s top should align with the top of the container.
• android:layout_alignParentBottom
says the widget’s bottom should align with the bottom of the container.
• android:layout_alignParentLeft
says the widget’s left side should align with the left side of the container.
• android:layout_alignParentRight
says the widget’s right side should align with the right side of the container.
• android:layout_centerHorizontal
says the widget should be positioned horizontally at the center of the container.
• android:layout_centerVertical
says the widget should be positioned vertically at the center of the container.
• android:layout_centerInParent
says the widget should be positioned both horizontally and vertically at the center of the container.
All of these properties take a simple Boolean value (true
or false
).
Note that the padding of the widget is taken into account when performing these various alignments. The alignments are based on the widget’s overall cell (a combination of its natural space plus the padding).
The remaining properties of relevance to RelativeLayout
take as a value the identity of a widget in the container. To identify and reference widgets this way, follow these steps:
1. Put identifiers (android:id
attributes) on all elements that you will need to address, of the form @+id/...
.
2. Reference other widgets using the same identifier value without the plus sign (@id/...
).
For example, if Widget A is identified as @+id/widget_a
, Widget B can refer to Widget A in one of its own properties via the identifier @id/widget_a
.
There are four properties that control position of a widget in relation to other widgets:
• android:layout_above
indicates that the widget should be placed above the widget referenced in the property.
• android:layout_below
indicates that the widget should be placed below the widget referenced in the property.
• android:layout_toLeftOf
indicates that the widget should be placed to the left of the widget referenced in the property.
• android:layout_toRightOf
indicates that the widget should be placed to the right of the widget referenced in the property.
There are five additional properties that can control one widget’s alignment relative to another:
• android:layout_alignTop
indicates that the widget’s top should be aligned with the top of the widget referenced in the property.
• android:layout_alignBottom
indicates that the widget’s bottom should be aligned with the bottom of the widget referenced in the property.
• android:layout_alignLeft
indicates that the widget’s left side should be aligned with the left side of the widget referenced in the property.
• android:layout_alignRight
indicates that the widget’s right side should be aligned with the right side of the widget referenced in the property.
• android:layout_alignBaseline
indicates that the baselines of the two widgets should be aligned.
The last property in the list is useful for aligning labels and fields so that the text appears “natural.” Since fields have a box around them and labels do not, android:layout_alignTop
will align the top of the field’s box with the top of the label, which will cause the text of the label to be higher on-screen than the text entered into the field.