Android Layouts

Layout is a type of a GroupView. It holds views in a certain layout on the screen.

Layouts can be declared in two ways:

Declare UI elements in XML –
Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

Instantiate layout elements at runtime –
your application can create View and ViewGroup objects (and manipulate their properties) programmatically.
The ViewGroup class serves as the base for subclasses called “layouts,” which offer different kinds of layout architecture, like linear, tabular and relative.

XML Layouts –
Each layout file must contain exactly one root element, which must be a View or ViewGroup object.
Once you’ve defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout.
After you’ve declared your layout in XML, save the file with the .xml extension, in your Android project’s res/layout/ directory, so it will properly compile.

Layout Examples
– Linear Layout
– Relative Layout
– Table Layout
– Tab Layout
– List View
– Grid View

Adding a Layout – XML example – adding to the layout 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"
    android:id="@+id/outer"
    android:orientation="vertical">
</LinearLayout>

The layout xml is placed in <project_path>/res/layouts

Adding a Layout – Code example (implemented in an Activity class).

LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setPadding(20,20,20,20);
innerLayout.setGravity(Gravity.CENTER);
outerLayout.addView(innerLayout );