Thu. Feb 27th, 2020

How to use a Linear Layout in android?

1 min read

LinearLayout is view group, which acts as a container for all the views so that all the views can be arranged in a direction either vertically (from top to bottom) or horizontally (from left to right) by the help of the attribute we call orientation

LinearLayout Android
Image 1

In the above image 1, the blue color rectangle is depicting a view group and all the rectangle with orange color inside view group are views which are placed horizontally in the same direction. Let’s Have one more Example to make it more clear to understand the concept.

In the above image, there is a box in which biscuits are kept so here the box acts as a container and the biscuits acts as views so the box is holding the food stuff likewise the LinearLayout holds the view.

Types of LinearLayout

There are two type of LinearLayout that are as follows:

1.Horizontal LinearLayout

2.Vertical LinearLayout

Attribute and Value of LinearLayout

Orientation

android:orientation=”vertical”


    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

android:orientation=”horizontal”

<?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="wrap_content"
android:orientation="horizontal" >

1.Horizontal LinearLayout

In Horizontal LinearLayout, the views are arranged horizontally from left to right. The attribute and property used in case of Vertical LinearLayout is Orientation=”horizontal”

<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/expandedListItem"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="view"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"
android:background="#455"
/>
<TextView
android:id="@+id/expandedListItem2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="view"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"
android:background="#455"
/>
<TextView
android:id="@+id/expandedListItem3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="view"
android:textColor="#fff"
android:layout_marginRight="5dp"
android:textSize="45dp"

2.Vertical LinearLayout

In vertical LinearLayout, the views are arranged vertically from top to bottom. The attribute and property used in case of Vertical LinearLayout is Orientation=”vertical”

<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/expandedListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hey I am A View(TextView)"
android:textSize="45dp"
android:background="#455"
android:textColor="#fff"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="@+id/expandedListItem1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hey I am A View(TextView)"
android:layout_marginTop="16dp"
android:textSize="45dp"
android:background="#455"
android:textColor="#fff"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="@+id/expandedListItem2"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:layout_height="wrap_content"
android:text="Hey I am A View(TextView)"
android:textSize="45dp"
android:background="#455"
android:textColor="#fff"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>

Properties Of LinearLayout

There are several properties in LinearLayout, By the use of which, we can make our User Interface look more attractive than before by using it as per our requirement. Some of the Attributes used in LinearLayout are as follows: 

1)android: orientation

2)android:layout_width

3)android:layout_height

4)android:layout_weight
5)android:layout_gravity
6)android:gravity
7)android:background
 

1 thought on “How to use a Linear Layout in android?

Leave a Reply

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

Copyright © All rights reserved. | Newsphere by AF themes.