Friday 12 October 2018

Linear Layout in Android

Linear layout is a simple layout used in android for layout designing. In the Linear layout all the elements are displayed in linear fashion means all the childs/elements of a linear layout are displayed according to its orientation. The value for orientation property can be either horizontal or vertical.
Types Of Linear Layout Orientation
There are two types of linear layout orientation:
  1. Vertical
  2. Horizontal
As the name specified these two orientations are used to arrange there child one after the other, in a line, either vertically or horizontally. Let’s we describe these in detail.
In this all the child are arranged vertically in a line one after the other. In below code snippets we have specified orientation “vertical” so the childs/views of this layout are displayed vertically.
2. Horizontal:
In this all the child are arranged horizontally in a line one after the other. In below code snippets we have specified orientation “horizontal” so the childs/views of this layout are displayed horizontally.

Important Note: All of the layout managers can be nested. This means that you can put a Relative Layout or Frame Layout as a child to Linear Layout.
Main Attributes In Linear Layout:
Now let’s  we discuss about the attributes that helps us to configure a linear layout and its child controls. Some of the most important attributes you will use with linear layout include:
1. orientation: The orientation attribute used to set the childs/views horizontally or vertically. In Linear layout default orientation is vertical.

Example:  Orientation vertical:
Example: Orientation Horizontal:
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the layout like left, right, center, top, bottom etc.
Example: We have set gravity right for linear layout. So the buttons gets align from right side in Horizontal orientation.

3. layout_weight: The layout weight attribute specify each child control’s relative importance within the parent linear layout.
Example: weight property for button in linear layout. In the below example one button is of weight 2 and other is of weight 1.
In the layout image you can notice Button with weight 2 gets more size related the other.

4. weightSum: weightSum is the sum up of all the child attributes weight. This attribute is required if we define weight property of the childs.
Example: In the same above example of weight, we can define weightSum value 3.

Example of Linear Layout:
Now lets design 2 linear layout UI. First we have designed using weight attribute and second without using it. So below layout output will clear the difference between them:

Example 1: First we will design Android Linear Layout without using weight property
In this example we have used one TextView and 4 Button. The orientation is set to vertical.
Below is the code of activity_main.xml
Output Screen:


Post a Comment

Popular Posts


AI (1) Android (23) AngularJS (1) Assembly Language (2) book (1) Books (33) C (76) C# (12) C++ (82) Course (47) Coursera (6) Cybersecurity (4) Data Science (19) Data Strucures (4) Deep Learning (4) Django (1) Downloads (3) Engineering (13) Excel (1) flutter (1) FPL (17) Google (10) Hadoop (1) HTML&CSS (41) IBM (1) IS (25) Java (90) Leet Code (4) Machine Learning (1) Meta (2) Pandas (2) PHP (20) Projects (26) Python (577) Python Coding Challenge (25) R (69) Selenium Webdriver (2) Software (15) SQL (28)


Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses