Learn about android layouts

When you first start with android development the verry first thing that  you have to learn is how the layouts work in android. There are several layout in android but we will be focusing on three main layouts im android that is

  1. Linear layout
  2. Relative layout
  3. Constraint layout
We will be covering Only Linear layout on this post.
Technical Coding

In android Linear Layout is the most simple layout of all. You arrange your views with linear layout with ease. Linear layout is two types - Horizontal & Vertical.

With Horizontal layout you can arrange your views horizontally, same goes for the Vertical Layout you can arrange your view vertically with it.
Technical Coding

These two type of layout can be decided with an attribute called android:orientation=""
the value you will enter in "" marks is gonna be type of this layout.This layout that arranges other views either horizontally in a single column or vertically in a single row.
Example: Horizontal layout android:orientation="horizontal"  & for Vertical layout android:orientation="vertical
You can also change the background color of this layout android:backgroundColor=""
use hexa coded or you can select from the android library.

You can set android:layout_weight on individual child views to specify how linear layout divides remaining space amongst the views it contains.

XML Attributes

android:orientation
Should the layout be a column or a row? Use "horizontal" for a row, "vertical" for a column. The default is horizontal.

The following code shows how to include a linear layout in your layout XML file:

                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:paddingLeft="16dp"
                     android:paddingRight="16dp"
                     android:orientation="horizontal"
                     android:gravity="center">

                                         [ Here you can place your views ]

            </LinearLayout>

android:gravity
Specifies how an object should position its content, on both the X and Y axes, within its own bounds. Its has more value


bottom             Push object to the bottom of its container, not changing its size.

center             Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal   Place object in the horizontal center of its container, not changing its size.
center_vertical     Place object in the vertical center of its container, not changing its size.
clip_horizontal     Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
clip_vertical     Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
end     Push object to the end of its container, not changing its size.
fill             Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal     Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical     Grow the vertical size of the object if needed so it completely fills its container.
left             Push object to the left of its container, not changing its size.
right             Push object to the right of its container, not changing its size.
start             Push object to the beginning of its container, not changing its size.
top             Push object to the top of its container, not changing its size.

android:divider
Drawable to use as a vertical divider between buttons.


You can also watch the video for more understanding

Post a Comment

0 Comments