What Is Android Layouts?

Author: Vasundhara Infotech

We provide mobile Application Development company services for every kind of business as fashion, wellness, shopping, E-commerce, and more. For many different reputed industries, we have created a dynamic, compelling, and user-friendly application for the Android platform.

The name suggests, all the elements are arranged in relative fashion it means element arrange itself relative to other elements or it's parent element.

1 Introduction

The most commonly used layouts are:

  1. Linear Layout
  2. Relative Layout

There are several other layouts such as:

  1. Table Layout
  2. Frame Layout
  3. List View Layout
  4. Grid View Layout
2 1. Linear Layout :-

In a linear layout, as the name suggests, all the elements are displayed in a linear fashion.

Linear Layout are arranged in two ways:-

  1. Horizontally
  2. Vertically.

This behaviour is set in android: orientation which is an attribute of the node LinearLayout.

Example :

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Registration"

android:gravity="center"

android:textStyle="bold"

android:textSize="@dimen/_20sdp"

android:textColor="#000000"

android:layout_margin="5dp"/>

android:id="@+id/userimage"

android:layout_width="@dimen/_80sdp"

android:layout_height="@dimen/_80sdp"

android:layout_gravity="center"

android:scaleType="centerCrop"

android:src="@mipmap/ic_launcher"/>

android:id="@+id/image_upload"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Upload Image"

android:textColor="#000000"

android:textStyle="bold"

android:gravity="center"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_margin="@dimen/_5sdp">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.20"

android:text="Name : "

android:textSize="@dimen/_20sdp"

android:textColor="#000000"

android:gravity="center"/>

android:id="@+id/ed_name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.50"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="@dimen/_5sdp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.20"

android:text="Email : "

android:textSize="@dimen/_20sdp"

android:textColor="#000000"

android:gravity="center"/>

android:id="@+id/ed_email"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:inputType="textEmailAddress"

android:layout_weight="0.50"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/btn_submit"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="Submit"

android:layout_weight="0.50"/>

android:id="@+id/btn_second"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="User Detail"

android:layout_weight="0.50"

android:layout_gravity="center"/>

OutPut:-

3 3. Table Layout :-

Table layouts in Android work like HTML table layouts work.

In the table layout, all layout arrange in row and column fashion.

Example :

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">

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:gravity="center_horizontal">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="18dp"

android:text="LIME"

android:layout_span="3"

android:padding="18dip"

android:background="#c6ff00"

android:textColor="#000"/>

android:id="@+id/tableRow1"

android:layout_height="wrap_content"

android:layout_width="match_parent">

android:id="@+id/TextView0"

android:text="BLUE "

android:layout_weight="1"

android:background="#84ffff"

android:textColor="#000000"

android:padding="20dip"

android:gravity="center"/>

android:id="@+id/TextView"

android:text="TEAL"

android:layout_weight="1"

android:background="#64ffda"

android:textColor="#000000"

android:padding="20dip"

android:gravity="center"/>

android:id="@+id/TextVie"

android:text="GREEN"

android:layout_weight="1"

android:background="#69f0ae"

android:textColor="#000000"

android:padding="20dip"

android:gravity="center"/>

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:gravity="center_horizontal">

android:id="@+id/TextView00"

android:text="YELLOW"

android:layout_weight="1"

android:background="#ffff00"

android:textColor="#000000"

android:padding="20dip"

android:gravity="center"/>

android:id="@+id/TextView01"

android:text="ORANGE"

android:layout_weight="1"

android:background="#ff6e40"

android:textColor="#000000"

android:padding="20dip"

android:gravity="center"/>

Out Put :-

4 2. Relative Layout :-

The name suggests, all the elements are arranged in relative fashion it means element arrange itself relative to other elements or it's parent element.

Example :

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge"

android:text="SIGN IN"

android:id="@+id/textView3"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

android:id="@+id/userName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="@dimen/_10sdp"

android:layout_marginTop="110dp"

android:text="UserName:"

android:textColor="#000000"

android:textSize="20sp" />

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/userName"

android:layout_margin="@dimen/_10sdp"

android:text="Password:"

android:textColor="#000000"

android:textSize="20sp" />

android:id="@+id/edt_userName"

android:layout_width="fill_parent"

android:layout_height="40dp"

android:layout_marginLeft="@dimen/_10sdp"

android:layout_marginTop="100dp"

android:layout_toRightOf="@+id/userName"

android:hint="User Name" />

android:layout_width="fill_parent"

android:layout_height="40dp"

android:layout_below="@+id/edt_userName"

android:layout_centerVertical="true"

android:layout_toRightOf="@+id/password"

android:hint="Password" />

android:id="@+id/btnLogin"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/password"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"

android:background="#03B424"

android:text="Login"

android:textColor="#ffffff"

android:textStyle="bold" />

OutPut:-

5 4. Frame Layout :-

FrameLayout should be used to hold a single child view because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

Frame Layout is designed to block out an area on the screen to display a single item.

Example :

Out Put:-

6 5. List View :-

Android Listview is one kind of view that combines several items.

List view display item in vertical Scrollable list fashion. In Listview items are inserted using the adapter that pulls content from a source such as an array or database.

Example :

Main XML file named layout_listview

Main XML file named layout_listview_item

Java Code : ListviewActivity

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class ListViewActivity extends AppCompatActivity implements View.OnClickListener {

Button linear,relative,frame,table,list,grid;

String[] colors = {"Red","Yellow","Pink","Black",

"Orange","Blue","Gray","White "};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.layout_listview);

ArrayAdapter adapter = new ArrayAdapter(this,

R.layout.layout_listview_item, colors);

ListView listView = (ListView) findViewById(R.id.mobile_list);

listView.setAdapter(adapter);

}

@Override

public void onClick(View view) { }

}

7 6. Grid View Layout:-

Android GridView shows items in two-dimensional scrolling grid (rows & columns) and the grid items are not necessarily predetermined but they automatically inserted to the layout using a ListAdapter.

Example :

XML File: layout_gridview

android:id="@+id/gridview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:columnWidth="90dp"

android:numColumns="auto_fit"

android:verticalSpacing="10dp"

android:horizontalSpacing="10dp"

android:stretchMode="columnWidth"

android:gravity="center"

/>

Java Code : GridViewActivity

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.GridView;

public class GridViewActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.layout_gridview);

GridView gridview = (GridView) findViewById(R.id.gridview);

gridview.setAdapter(new GridAdapter(this));

}

}

Adapter: GridAdapter

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.ImageView;

public class GridAdapter extends BaseAdapter {

private Context mContext;

// Constructor

public GridAdapter(Context c) {

mContext = c;

}

public int getCount() {

return mThumbIds.length;

}

public Object getItem(int position) {

return null;

}

public long getItemId(int position) {

return 0;

}

// create a new ImageView for each item referenced by the Adapter

public View getView(int position, View convertView, ViewGroup parent) {

ImageView imageView;

if (convertView == null) {

imageView = new ImageView(mContext);

imageView.setLayoutParams(new GridView.LayoutParams(250, 250));

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

imageView.setPadding(8, 8, 8, 8);

}

else

{

imageView = (ImageView) convertView;

}

imageView.setImageResource(mThumbIds[position]);

return imageView;

}

// Keep all Images in array

public Integer[] mThumbIds = {

R.drawable.album_1,

R.drawable.album_2,

R.drawable.album_3,

R.drawable.album_4,

R.drawable.album_5,

R.drawable.album_6,

R.drawable.album_7,

R.drawable.album_8,

R.drawable.album_9,

};

}