Layouts: Organizing The Screen Main Layout Strategies: Android Apps Programming
Layouts: Organizing The Screen Main Layout Strategies: Android Apps Programming
Layouts: Organizing The Screen Main Layout Strategies: Android Apps Programming
Example: <LinearLayout
android:layout_width=match_parent
android:layout_height=wrap_content
android:gravity=center_horizontal
android:background=@color/color_1>
...
</LinearLayout>
Size
android:layout_height, android:layout_width
match_parent fill the parent space (minus padding)
Renamed from fill_parent in older versions
android:layout_weight
It controls the resizing behavior of its child Views. The value is either 0 or 1.
Using a weight of 1 makes the View stretch, while using 0 will make that View
just as big as needed.
Alignment
android:layout_gravity
How the View is aligned within containing View.
android:gravity
How the text or components inside the View are aligned.
Possible values:
top, bottom, left, right, center_vertical, center_horizontal, center, fill_vertical,
fill_horizontal, fill, clip_vertical, clip_horizontal
It controls the spacing between Views within a layout. Values are numbers with
units as above.
ID
android:id
Color
android:background (color or image, for any layout)
android:textColor(e.g. for TextView or Buttton)
Example: android:background=#ff8D8D8D
Click Handler
android:onClick
Should be a public method in mainActivity that takes a View (the thing clicked) as
argument and returns void.
Types of Layout:
LinearLayout
Put components in a single row or single column;
android:orientation
horizontal (a row) or vertical (a column)
android:gravity
How the Views inside are aligned.
Possible values:
top, bottom, left, right, center_vertical, center_horizontal, center,
fill_vertical, fill_horizontal, fill, clip_vertical, clip_horizontal
RelativeLayout
Position other components relative to those components
TableLayout
Put widgets or nested layouts in a grid. Like HTML tables, the number of rows and
columns is determined automatically, not explicitly specified. Components are
usually placed inside TableRow.
android:stretchColumns
An index or comma-separated list of indexes. Specifies the column or columns
that should be stretched wider if the table is narrower than its parent. Indexes are 0-
based.
android:shrinkColumns
Column(s) that should be shrunk if table is wider than parent.
android:collapseColumns
Column(s) to be totally left out. Can be programmatically putback in later.
android:layout_column
Normally, elements are placed in left-to-right order. However, you can use
android:layout_column to specify an exact column, and thus leave earlier
columns empty.
android:layout_span
The number of columns the element should straddle, like colspan for HTML
tables. There is nothing equivalent to HTMLs rowspan; you must use nested
table instead.
AbsoluteLayout
From older versions; now depracated; use RelativeLayout
FrameLayout
For formatting a single item. Usually used explicitly with
ViewGroups
TabHost
Used internally by other layouts. Combines tabs with switching Activities
ListView
GridView
A GridView control is simply a container that holds a list of View objects. GridView is
a type of control called an AdapterView. Controls derived from AdapterView are
excellent for repeated items with identical layouts. To effectively use the GridView
Control, an adapter must be created as well as the
ScrollView
To change Orientation
In activity_main.xml : android:screenOrientation=portrait
android:configChanges=orientation|keyboardHidden
It specifies the screen orientation for the app. 2nd line makes sure that screen
orientations do not occur when either
The hardware keyboard is slid open or closed on a device
The software keyboard is enabled or disabled.
Android 3.0 introduced the idea of minimum screen widths and heights to
determine the dynamic layout loading. Aside from declaring small, med or large screen
widths, declare it in Density Independent Pixels (DPs). The name of the folder
determines in the size the layout applied for. The width or height is specified with a w or
h, followed by the dimensions in DPs. Screens larger than the specified width or height
load the layouts in the folder.
Screen Size
Small screens are at least 426dp x 320dp
Normal screens are at least 470dp x 320dp
Large screen are at least 640dp x 480dp
xLarge screens are at least 960dp x 720dp