LinearLayout in Android | LinearLayout Android Example

LinearLayout in Android – Introduction

LinearLayout in Android – The LinearLayout aligns all the views in a single direction, either horizontal or vertical. But the direction can be set by the user using the orientation attribute of the LinearLayout element. In LinearLayout, all the views are placed one after the other, so in case you have set the orientation as vertical, then only one view would be displayed in a row.

Create activity_linear_layout file in XML

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent” // cover full screen
android:layout_height=”match_parent” // cover full screen
android:orientation=”vertical” // screen orientation will be vertical
android:gravity=”center” // all the elements set to be center
tools:context=”.linearLayout” >

<TextView
android:layout_width=”wrap_content” // cover the width of the text view, not full screen
android:layout_height=”wrap_content” // cover the width of the text view, not full screen
android:text=”This is Horizontal Layout Example”
android:layout_gravity=”center” // element is set to center
android:layout_marginLeft=”10dp” // It is used to give space from top screen
android:textSize=”25sp”/>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_margin=”15dp”
android:gravity=”center”
android:orientation=”horizontal”> // screen orientation will be horizontal

<Button
android:id=”@+id/linearButton1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Horizontal “
android:layout_marginRight=”10dp”
/>

<Button
android:id=”@+id/linearButton2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Horizontal”
android:layout_marginLeft=”10dp”/>

</LinearLayout>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This is Vertical Layout Example”
android:layout_gravity=”center”
android:layout_marginLeft=”10dp”
android:textSize=”25sp”
android:layout_marginTop=”15sp”/>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_margin=”10dp”
android:gravity=”center”
android:orientation=”vertical”>

<Button
android:id=”@+id/linearButton3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Vertical”/>

<Button
android:id=”@+id/linearButton4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Vertical”/>

</LinearLayout>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This is the Grid Layout Example”
android:layout_gravity=”center”
android:layout_marginLeft=”10dp”
android:textSize=”25sp”
android:layout_marginTop=”15sp”/>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginLeft=”10dp”
android:layout_marginRight=”10dp”
android:layout_marginTop=”5dp”
android:orientation=”horizontal”>

<LinearLayout
android:layout_height=”match_parent”
android:layout_width=”0dp”
android:layout_weight=”1″
android:orientation=”vertical”
android:gravity=”center”>

<Button
android:id=”@+id/linearButton5″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Grid”/>

</LinearLayout>

<LinearLayout
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:gravity=”center”>

<Button
android:id=”@+id/linearButton6″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Grid”/>

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginLeft=”10dp”
android:layout_marginRight=”10dp”
android:layout_marginTop=”5dp”
android:orientation=”horizontal”>

<LinearLayout
android:layout_width=”0dp”
android:layout_weight=”1″
android:layout_height=”match_parent”
android:orientation=”vertical”
android:gravity=”center”>

<Button
android:id=”@+id/linearButton7″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Grid”/>

</LinearLayout>

<LinearLayout
android:layout_width=”0dp”
android:layout_weight=”1″
android:layout_height=”match_parent”
android:orientation=”vertical”
android:gravity=”center”>

<Button
android:id=”@+id/linearButton8″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Grid”/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

Create linearLayout File in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class linearLayout extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_layout);
}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.linearLayout”></activity>
</application>

</manifest>

LinearLayout in Android – Output

LinearLayout in Android | LinearLayout Android Example

Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Online Surveys – Earn Real Money by Online Surveys
Kids Books – 10+ books for Kids | Age group 2 – 10 | Improve knowledge & Productivity

Leave a Reply

Your email address will not be published.