LinearLayout and its Important Attributes with Examples in Android - GeeksforGeeks

14 Oct, 2020
LinearLayout is the most basic layout in android studio, that aligns all the children sequentially either in a horizontal manner or a vertical manner by specifying the android:orientation attribute. If one applies android:orientation=”vertical” then elements will be arranged one after another in a vertical manner and If you apply android:orientation=”horizontal” then elements will be arranged one after another in a horizontal manner.
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"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity"></LinearLayout>
Attributes
Description
It is defined individually to the child’s views to specify how LinearLayout 
divides the remaining space amongst the views it contains
It specifies how an object should position its content on its X and Y axes. 
Possible values are – center_vertical, fill, center, bottom, end, etc.
Sets the gravity of the View or Layout relative to its parent. 
Possible values are – center_vertical, fill, center, bottom, end, etc.
This must be a boolean value, either “true” or “false” and prevents the layout 
from aligning its children’s baselines.
1. How to arrange children views in a vertical manner 
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"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <!-- Add vertical in the android:orientation-->      <!-- Add Button-->    <Button        android:layout_width="match_parent"        android:layout_margin="10dp"        android:layout_height="wrap_content"/>      <!-- Add Button-->    <Button        android:layout_width="match_parent"        android:layout_margin="10dp"        android:layout_height="wrap_content"/>      <!-- Add Button-->    <Button        android:layout_width="match_parent"        android:layout_margin="10dp"        android:layout_height="wrap_content"/>  </LinearLayout>
Output UI:
2. How to arrange children views in a horizontal manner
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context=".MainActivity">    <!-- Add horizontal in the android:orientation-->      <!-- Add Button-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp" />          <!-- Add Button-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp" />          <!-- Add Button-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp" />  </LinearLayout>
Output UI:
3. How to use layout_weight and weightSum
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:weightSum="3"    tools:context=".MainActivity">    <!-- Add value in the android:weightSum-->    <!-- Add horizontal in the android:orientation-->      <!-- Add Button-->    <!-- Add value in the android:layout_weight-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1" />          <!-- Add Button-->    <!-- Add value in the android:layout_weight-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1" />          <!-- Add Button-->    <!-- Add value in the android:layout_weight-->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1" />  </LinearLayout>
Output UI:
4. How to use gravity 
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:weightSum="3"    tools:context=".MainActivity">    <!-- Add value in the android:weightSum-->    <!-- Add horizontal in the android:orientation-->      <!-- Add Button-->    <!-- Add value in the android:gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1"        android:gravity="bottom|center"        android:text="GFG" />          <!-- Add Button-->    <!-- Add value in the android:gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1"        android:gravity="center"        android:text="GFG" />          <!-- Add Button-->    <!-- Add value in the android:gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:layout_weight="1"        android:gravity="center|top"        android:text="GFG" />  </LinearLayout>
Output UI:
5. How to use layout_gravity 
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <!-- Add vertical in the android:orientation-->      <!-- Add Button-->    <!-- Add value in the layout_gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="10dp" />          <!-- Add Button-->    <!-- Add value in the layout_gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:layout_margin="10dp" />          <!-- Add Button-->    <!-- Add value in the layout_gravity -->    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="left"        android:layout_margin="10dp" />  </LinearLayout>
Output UI:
android
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
How to View and Locate SQLite Database in Android Studio?
Navigation Drawer in Android
Retrofit with Kotlin Coroutine in Android
Flutter - Custom Bottom Navigation Bar
Android Listview in Java with Example
How to Add and Customize Back Button of Action Bar in Android?