Android UI Layouts

  • Android UI Layouts

Layouts in Android

In this article, Let’s discuss about the different view layouts in an android mobile application.

Definition,

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget.It may contain any type of widgets such as buttons, labels, textboxes, and so on.

Android UI Layout which you will use in almost all the Android applications to provide different view, look and feel. The  seven different  Layouts are,

  1. Linear Layout
  2. Relative Layout
  3. Absolute Layout
  4. Table Layout
  5. Frame Layout
  6. List view
  7. Grid view

1.Linear Layout

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.It also supports assigning a weight to individual children with the android:layout_weight attribute. Either Horizontally or Vertically and this behavior is set in android:orientation which is an attribute of the node LinearLayout.

linear layout

Vertical layout xml snippet

<LinearLayout android:orientation=”vertical”> …. </LinearLayout>

Horizontal layout xml snippet

<LinearLayout android:orientation=”horizontal”> …. </LinearLayout>

2.RelativeLayout

RelativeLayout specify how child views are positioned relative to each other. Then position of each view can be specified as relative to sibling elements. you can use “below,above, right and left” to arrange the component position

relativelayout

RelativeLayout xml snippet

<RelativeLayout Button android:layout_toRightOf=”@id/btn”

android:layout_alignTop=”@id/btn” ..></Button>

</ RelativeLayout >

3.Table Layout

TableLayout is a view that groups views into rows and columns. Its very easy to understand

table1

Table Layout xml snippet

<Table Layout>

<TableRow

android:layout_height=”wrap_content”

android:layout_width=”fill_parent”

android:gravity=”center_horizontal”>

<TextView

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:textSize=”13dp” android:text=”Row 1″  android:layout_span=”4″

android:padding=”13dip” android:background=”#0ffff”

android:textColor=”#f0f0f0″/>

</TableRow>

</Table Layout>

Must See : Why Learn Android ? 

4.Absolute Layout

AbsoluteLayout enables you to specify the exact location of its children. It has  are less flexible and harder to maintain than other types of layouts without absolute positioning.

absolute1 (1)

Absolute Layout xml snippet

<AbsoluteLayout

android:layout_width=”50dp”

android:layout_height=”wrap_content

android:layout_x=”25px”

android:layout_y=”29px”>

</AbsoluteLayout>

5.Frame Layout

The FrameLayout is a placeholder on screen that you can use to display a single view.

framelayout

FrameLayout xml snippet

<FrameLayout

TextView

android:text=”Frame Demo”

android:textSize=”30px”

android:textStyle=”bold”

android:layout_height=”fill_parent”

android:layout_width=”fill_parent”

android:gravity=”center”/>

</FrameLayout>

6.ListView

Listview is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an adapter that pulls the content from a source such as an array or database query and converts each item result into a view that’s placed into the list.

listview

ListView xml snippet

<LinearLayout

android:layout_width=”match_parent”

android:layout_height=” wrap_content ”

android:orientation=”vertical”>

<ListView

android:id=”@+id/months_list”

android:layout_width=”match_parent”

android:layout_height=”wrap_content” >

</ListView>

</LinearLayout>

 

7.Grid view

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

gridview1

Grid view xml snippet

<RelativeLayout

android:layout_height=” wrap_content ”

android:layout_width=”match_parent”

android:background=”#f0f0f0″>

<GridView

android:id=”@+id/gridView”

android:layout_margin=”6 dp”

android:columnWidth=”50 dp”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:drawSelectorOnTop=”true”

android:gravity=”center”

android:numColumns=”auto_fit”

android:stretchMode=”columnWidth”

android:verticalSpacing=”4dp”

android:focusable=”true”

android:clickable=”true”/>

</RelativeLayout>

In this article you have seen various layouts available in android to create your UI.This helps us create apps while keeping the layout and logic completely separate. This makes it very easy for us to change the layout even after the application is written, without having any changes in the programming logic.

Stay tuned!