In Android, the user interface of an app is made up of a hierarchy of objects called “views” and “view groups”. These objects are usually instantiated from layout resource files that are written in XML. Let’s explore the different types of layouts available in Android.
LinearLayout
A LinearLayout
is a view group that aligns all its child views either vertically or horizontally. You can specify the layout direction via the android:orientation
attribute.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Add child views here -->
</LinearLayout>
RelativeLayout
A RelativeLayout
is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements or relative to the parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add child views here -->
</RelativeLayout>
FrameLayout
The FrameLayout
is a view group that pins each child’s view to the top left corner of the screen. It’s useful for stacking views on top of each other, like cards in a deck.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add child views here -->
</FrameLayout>
TableLayout
A TableLayout
is a view group that lays out its children in a grid of rows and columns. It’s useful for displaying tabular data.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add child views here -->
</TableLayout>
GridLayout
A GridLayout
is a view group that aligns all its child views in a grid. It allows for more flexibility than TableLayout
as it does not require each row to have the same number of cells.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add child views here -->
</GridLayout>
Sr.No | Attribute & Description |
---|---|
1 | android:id This is the ID which uniquely identifies the view. |
2 | android:layout_width This is the width of the layout. |
3 | android:layout_height This is the height of the layout |
4 | android:layout_marginTop This is the extra space on the top side of the layout. |
5 | android:layout_marginBottom This is the extra space on the bottom side of the layout. |
6 | android:layout_marginStart This is the extra space on the left side of the layout. |
7 | android:layout_marginEnd This is the extra space on the right side of the layout. |
8 | android:layout_gravity This specifies how child Views are positioned. |
9 | android:layout_weight This specifies how much of the extra space in the layout should be allocated to the View. |
10 | android:layout_x This specifies the x-coordinate of the layout. |
11 | android:layout_y This specifies the y-coordinate of the layout. |
12 | android:layout_width This is the width of the layout. |
13 | android:paddingStart This is the left padding filled for the layout. |
14 | android:paddingEnd This is the right padding filled for the layout. |
15 | android:paddingTop This is the top padding filled for the layout. |
16 | android:paddingBottom This is the bottom padding filled for the layout. |