Recipe 2.10 Using LinearLayout for View Positioning

Android Versions
Level 1 and above
Permissions
None
Source Code to Download from Wrox.com
UsingLinearLayout.zip

An activity contains Views and ViewGroups. A view is a widget that has an appearance on screen. Examples of views are Buttons, TextViews, and EditTexts. A view derives from the base class android.view.View.

One or more Views can be grouped together into a ViewGroup. A ViewGroup (which is itself a special type of view) provides the layout in which you can order the appearance and sequence of views. Examples of ViewGroups include LinearLayout and FrameLayout. A ViewGroup derives from the base class android.view.ViewGroup.

Android supports the following ViewGroups:

  • LinearLayout
  • TableLayout
  • RelativeLayout
  • FrameLayout
  • ScrollView

The following recipes describe each of these ViewGroups in more detail. Note that in practice it is common to combine different types of layouts to create the UI you want. This recipe discusses the LinearLayout ViewGroup.

Solution

You have several options when arranging views using the LinearLayout:

  • They can be arranged in a single column or a single row.
  • Child views can be arranged either vertically or horizontally.
  • Views can be aligned to certain sides of a screen, using weight specifications.

The following sections discuss these in more details.

Orientations

You can set the orientation to display horizontally or vertically, so items appear in a single column or a single ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.