Android Layouts: Linear Layout, Relative Layout and Table Layout
Android Layouts: Linear Layout, Relative Layout and Table Layout
Android Layouts: Linear Layout, Relative Layout and Table Layout
In this post I will be discussing about the different view layouts in an android mobile application. The six different
layouts are
1. Linear Layout
2. Relative Layout
3. Table Layout
4. Grid Layout
5. Tab Layout
6. List View
Android allows you to create view layouts using simple XML file (we can also create a layout using java code). All the
layouts must be placed in /re s/la yo ut folder.
1. Linear Layout
In a linear layout, like the name suggests, all the elements are displayed in
a linear fashion(below is an example of the linear layouts),
either H ori zo nt all y or Ver tica lly and this behavior is set
in a ndro i d: orie nt at io n which is an attribute of the node
LinearLayout.
Example of Vertical layout snippet
<LinearLayout android:orientation="vertical"> .... </LinearLayout>
Example of Horizontal layout snippet
<LinearLayout android:orientation="horizontal"> .... </LinearLayout>
Now that we know the two types of linear layouts, here are the steps you need to follow to create them
1. Create a new project File -> New -> Android Project
2. In Package Explorer right click on re s/l ay out folder and create a new Android XML File and name it as you wish.
I am naming it as “li ne ar _la yo ut.x ml ”
res/layout -> Right Click -> New -> Android XML File
3. Now open newly created xml file (in my case “line ar_l ay o ut .x ml ”) and type the following code.
<?xml version="1.0" encoding="utf-8"?>
<!-- Parent linear layout with vertical orientation -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
4. To set this newly created view as the initial view of your app, Open your MainActivity.java file. You would see the
following line inside the onCrea te function se t Co nte ntVi ew( R.l ay o ut . mai n) .
Change R .l ay o ut . mai n to R.l ay out. yo ur li nea rview na me . In my case its R. la yo ut .li ne ar _l ay o ut
package com.example.androidlayouts;
import android.app.Activity;
import android.os.Bundle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout);
}
}
5. To run the application, right click on the project -> Run As -> 1. Android Application. You should see your newly
created linear layout in the emulator.
2. Relative Layout
In a relative layout every element arranges itself relative to other elements or a parent element.
As an example, lets consider the layout defined below. The “Ca ncel ” button is placed relatively, to the rig ht of the
“L o gi n ” button par allel y . Here is the code snippet that achieves the mentioned alignment (Right of Loginbutton
parallely)
Example code snippet
<Button android:id="@+id/btnLogin" ..></Button>
<Button android:layout_toRightOf="@id/btnLogin"
android:layout_alignTop="@id/btnLogin" ..></Button>
3. Table Layout
Table layouts in Android works in the same way HTML table layouts work. You
can divide your layouts into r ows and col umns . Its very easy to understand. The
image below should give you an idea.
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*" android:stretchColumns="*" android:background="#ffffff">
<!-- Row 1 with single column -->
<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="18dp" android:text="Row 1" android:layout_span="3"
android:padding="18dip" android:background="#b0b0b0"
android:textColor="#000"/>
</TableRow>
<TextView
android:id="@+id/TextView04" android:text="Row 3 column 2"
android:layout_weight="1" android:background="#a09f9f"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
</TableRow>
</TableLayout>
4. Same like before open your MainActivity.java file and set the layout to your newly created table layout file. In my
case its R .la yo ut. ta bl e_ la yo ut
setContentView(R.layout.table_layout);
5. To run the application, right click on the project -> Run As -> 1. Android Application. You should see your newly
created table layout in the emulator.