Recipe 2.10 Using LinearLayout for View Positioning
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access