Example

With all that in mind, let's examine a typical "form" with a field, a label, plus a pair of buttons labeled "OK" and "Cancel".

Here is the XML layout, pulled from the Relative sample project:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="URL:"

android

paddingTop="5px"/>

<EditText

android

id="@+id/entry"

android

layout_width="fill_parent"

android

layout_height="wrap_content"

android

layout_toRight="@id/label"

android

layout_alignBaseline="@id/label"/>

<Button

android

id="@+id/ok"

android

layout_width="wrap_content"

android

layout_height="wrap_content"

android

layout_below="@id/entry"

android

layout_alignRight="@id/entry"

android

text="OK" />

<Button

android

layout_width="wrap_content"

android

layout_height="wrap_content"

android

layout_toLeft="@id/ok"

android

layout_alignTop="@id/ok"

android

text="Cancel" />

</RelativeLayout>

First, we open up the RelativeLayout. In this case, we want to use the full width of the screen (android:layout_width = "fill_parent"), only as much height as we need (android:layout_height = "wrap_content"), and have a 5-pixel pad between the boundaries of the container and its contents (android:padding = "5px").

Next, we define the label, which is fairly basic, except for its own 5-pixel padding (android: padding = "5px"). More on that in a moment.

After that, we add in the field. We want the field to be to the right of the label, have their texts aligned along the baseline, and for the field to take up the rest of this "row" in the layout. Those are handled by three properties:

• android:layout_toRight = "(Sid/label"

• android:layout_alignBaseline = "(Sid/label"

• android:layout_alignBaseline = "(Sid/label"

If we were to skip the 5-pixel padding on the label, we would find that the top of the field is clipped off. That's because of the 5-pixel padding on the container itself. The android:layout_alignBaseline = "(id/label" simply aligns the baselines of the label and field. The label, by default, has its top

aligned with the top of the parent. But the label is shorter than the field because of the field's box. Since the field is dependent on the label's position, and the label's position is already defined (because it appeared first in the XML), the field winds up being too high and has the top of its box clipped off by the container's padding.

You may find yourself running into these sorts of problems as you try to get your RelativeLayout to behave the way you want it to.

The solution to this conundrum, used in the XML layout shown above, is to give the label 5 pixels' of padding on the top. This pushes the label down far enough that the field will not get clipped.

Here are some "solutions" that do not work:

• You cannot use android:layout_alignParentTop on the field, because you cannot have two properties that both attempt to set the vertical position of the field. In this case, android:layout_alignParentTop conflicts with the later android:layout_alignBaseline = "(aid/label" property, and the last one in wins. So, you either have the top aligned properly or the baselines aligned properly, but not both.

• You cannot define the field first, then put the label to the left of the field, as the android:layout_width = "fill_parent" "eats up" the width of the "row", leaving no room for the label, so the label does not appear

Going back to the example, the OK button is set to be below the field (android: layout_below = "((id/entry") and have its right side align with the right side of the field (android:layout_alignRight = "((id/entry"). The Cancel button is set to be to the left of the OK button (android: layout_toLeft = "@id/ok") and have its top aligned with the OK button (android:layout_alignTop = "@id/ok").

With no changes to the auto-generated Java code, the emulator gives us:

Figure 14. The RelativeLayoutDemo sample application
Self Publishing

Self Publishing

Have you always wanted to write your own book and get it published? Discover How to Write, Print Sell Your Own Book! Have you always wanted to write your own book and get it published? If you have written a book or even if you have a yen to be a writer, you are probably already aware of the competition in the writing field.

Get My Free Ebook


Post a comment