What is Linear Layout in Android?

Published by Shivam Pandey on

app

Linear Layout is a simple layout and basically used for designing in android. All the elements of the linear layout are displayed in a linear fashion. In general, there child/elements displayed according to orientation. The orientation would be either vertical or horizontal as per need.

Types Of Linear Layout Orientation.

There are two types of orientation

  1. Vertical
  2. Horizontal

1. Vertical.

All the elements are arranged vertically in a line one after another. In below code snippet you will see how to use it.

 

2. Horizontal.

All the elements are arranged horizontally in a line one after another. In below code snippet you will see how to use it.

Main Attributes in Linear Layout.

1. Gravity

The gravity attribute is an optional attribute. Like the left, right, centre, top, bottom etc.

Example:

we have set gravity centre for linear layout. So the buttons get align to the middle in a vertical orientation in our layout, see below code snippet.

2. Layout_weight

The android:layout_weight attribute assign how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value. And then any remaining space in the view group is assigned to children in the proportion of their declared weight. The default weight is zero.

Example:

Weight property for a button in a linear layout. In the below example one button is of weight 3 and other is of weight 1.

3. Weight_Sum

android:weightSum attribute is the sum-up of all the child attributes weight. We need this attribute to define the weight property of the children.

 

THANK FOR READING THIS BLOG STAY SAFE AND KEEP CODING

CONNECT WITH THE AUTHOR ON LINKEDIN.

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT ANDROID DEVELOPMENT.

IF YOU WANT TO KNOW ABOUT XML IN ANDROID click here.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US