Recipe Controlling the Width and Height of UI Elements

This recipe shows how specifying the width and height of UI elements changes the overall layout. Each View object must specify a total width android:layout_width and total height android:layout_height in one of three ways:

■ exact dimension—Provides control, but does not scale to multiple screen types well.

■ wrap_content—Just big enough to enclose the contents of the element plus padding.

■ fill_parent—Size maximized to fill the element's parent, including padding.

Padding is the blank space surrounding an element, and defaults to zero if it is not specified. It is part of the size of an UI element and must be specified as an exact dimension, but can be specified using one of two types of attributes:

■ padding—Sets padding equal on all four sides of an element.

■ paddingLeft, paddingRight, paddingTop, paddingBottom—Sets padding on each side of an element separately

Another attribute is android:layout_weight, which can be assigned a number. It provides the Android system with a way to determine relative importance between different elements of a layout.

Listing 4.2 shows the main layout file as a linear layout with four buttons.This aligns them horizontally on the screen, as shown in Figure 4.4.

Listing 4.2 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="add" android:layout_width="wrap_content" android:layout_height="wrap_content" />

<Button android:text="subtract" android:layout_width="wrap_content" android:layout_height="wrap_content" />

<Button android:text="multiply" android:layout_width="wrap_content" android:layout_height="wrap_content" />

<Button android:text="divide" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

Figure 4.4 LinearLayout with four buttons aligned horizontally, as shown in Listing 4.2.

If the height of the "add" button is changed to fill_parent, the button fills the vertical space of its parent while keeping the words aligned. If the width of any button is changed to fill_parent, then all subsequent buttons in the horizontal layout are washed out. These are shown in Figure 4.5.

Figure 4.4 LinearLayout with four buttons aligned horizontally, as shown in Listing 4.2.

Figure 4.5 The fill_parent in height keeps the horizontal alignment, but a fill_parent in width washes out the remaining buttons.

Another thing to point out in Figure 4.4 is that the "multiply" and "divide" buttons have a portion of the last letter cut off.This can be fixed by appending a space to the text, such as "multiply" and "divide". However, a more general method to resolve this utilizes the layout.Take a look at the various button formats in Figure 4.6.

Figure 4.6 Various methods to tweak the layout of four buttons.

The four rows of buttons in Figure 4.6 are as follows:

■ The first row is the same as Listing 4.2, but with spaces appended to the end of each word.

■ In the second row, the layout width is changed to fill_parent for the last button, providing the space needed for the button, but it cannot be used for the earlier buttons on the line as evidenced by the right part of Figure 4.5:

<Button android:text="divide"

android:layout_width="fill_parent" android:layout_height="wrap_content"

■ In the third row, padding is added to the multiply button to make the button bigger, but it does not add this space to the word itself because it was declared as wrap_content:

<Button android:text="multiply"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingRight="20sp"

■ In the fourth row, all buttons use fill_parent, but also add layout_weight and assign it the same value for all buttons.This gives the most satisfying layout:

<Button android:text="add"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

<Button android:text="subtract"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

<Button android:text="multiply"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

<Button android:text="divide"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment