This chapter show how to lay out controls for different orientations as well as apply attributes such as Orientation, Height, Width, Padding, Weight, and Gravity to arrange the controls and their content.

A container is a view used to contain other views. Android offers a collection of view classes that act as containers for views. These container classes are called layouts, and as the name suggests, they decide the organization, size, and position of their children views.

Let’s start the chapter with an introduction to different layouts used in Android applications.

Introduction to Layouts

Layouts are basically containers for other items known as Views, which are displayed on the screen. Layouts help manage and arrange views as well. Layouts are defined in the form of XML files that cannot be changed by our code during runtime.

Table 3.1 shows the layout managers provided by the Android SDK.

Table 3.1. Android Layout Managers

Layout Manager

Description

LinearLayout

Organizes its children either horizontally or vertically

RelativeLayout

Organizes its children relative to one another or to the parent

AbsoluteLayout

Each child control is given a specific location within the bounds of the container

FrameLayout

Displays a single view; that is, the next view replaces the previous view and hence is used to dynamically change the children in the layout

TableLayout

Organizes its children in tabular form

GridLayout

Organizes its children in grid format

The containers or layouts listed in Table 3.1 are also known as ViewGroups as one or more Views are grouped and arranged in a desired manner through them. Besides the ViewGroups shown here Android supports one more ViewGroup known as ScrollView, which is discussed in Chapter 4, “Utilizing Resources and Media.”