LinearLayout and its Important Attributes with Examples in Android Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 26 Likes Like Report LinearLayout is one of the most basic layouts in android studio, that arranges multiple sub-views (UI elements) sequentially in a single direction i.e. horizontal or 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 (i.e. top to bottom) and If you apply android:orientation="horizontal" then elements will be arranged one after another in a horizontal manner (i.e. left to right). Sample Code of LinearLayout: 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"> <!--sub view 1--> <View android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--sub view 2--> <View android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--sub view 3--> <View android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> Some Important Attributes of LinearLayoutAttributesDescriptionandroid:idAssigns a unique id to the layout.android:orientationDefines the arrangements of sub-views in the layout. It is either "horizontal" or "vertical".android:layout_widthSets the width of the layoutandroid:layout_heightSets the height of the layoutandroid:layout_weightAssigned individually to the sub-views, it specifies how the parent layout divides the remaining space amongst the sub-views.android:weightSumDefined in the layout, it sets the total weight sum of all the sub-view inside the layout.android:layout_gravityAssigned to the sub-views, it sets the gravity of the view or layout relative to its parent. Possible values are - center, center_vertical, center_horizontal, fill, top, bottom, start, end, etc.android:baselineAlignedAssigns a Boolean value, which prevents the layout from aligning its children's baselines.Examples of LinearLayout1. How to arrange views in a vertical manner in LinearLayout 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"> <!-- Set Orientation as "vertical" --> <!--sub view 1--> <View android:id="@+id/view_1" android:background="@color/grey" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="100dp" /> <!--sub view 2--> <View android:id="@+id/view_2" android:background="@color/grey" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="100dp" /> <!--sub view 3--> <View android:id="@+id/view_3" android:background="@color/grey" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="100dp" /> <!--sub view 4--> <View android:id="@+id/view_4" android:background="@color/grey" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="100dp" /> </LinearLayout> Design UI: 2. How to arrange views in a horizontal manner in LinearLayout 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"> <!-- Set Orientation as "horizontal" --> <!--sub view 1--> <View android:id="@+id/view_1" android:background="@color/grey" android:layout_width="64dp" android:layout_margin="16dp" android:layout_height="wrap_content" /> <!--sub view 2--> <View android:id="@+id/view_2" android:background="@color/grey" android:layout_width="64dp" android:layout_margin="16dp" android:layout_height="wrap_content" /> <!--sub view 3--> <View android:id="@+id/view_3" android:background="@color/grey" android:layout_width="64dp" android:layout_margin="16dp" android:layout_height="wrap_content" /> <!--sub view 4--> <View android:id="@+id/view_4" android:background="@color/grey" android:layout_width="64dp" android:layout_margin="16dp" android:layout_height="wrap_content" /> </LinearLayout> Design UI: 3. How to use layout_weight and weightSum in LinearLayout 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" android:weightSum="5" tools:context=".MainActivity"> <!-- Set Orientation as "vertical" --> <!-- Set WeightSum as "3" --> <!--sub view 1--> <!--This view has more weight than others providing it with more space--> <TextView android:id="@+id/view_1" android:text="GeeksforGeeks" android:background="@color/black" android:textColor="@color/white" android:gravity="center" android:layout_weight="3" android:layout_width="match_parent" android:layout_margin="32dp" android:layout_height="wrap_content" /> <!--sub view 2--> <TextView android:id="@+id/view_2" android:text="GeeksforGeeks" android:background="@color/grey" android:textColor="@color/white" android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_margin="32dp" android:layout_height="wrap_content" /> <!--sub view 3--> <TextView android:id="@+id/view_3" android:text="GeeksforGeeks" android:background="@color/grey" android:textColor="@color/white" android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_margin="32dp" android:layout_height="wrap_content" /> </LinearLayout> Design UI: 4. How to use layout_gravity in LinearLayout 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"> <!-- Set Orientation as "vertical" --> <!-- Set WeightSum as "3" --> <!--sub view 1--> <!--Set the layout gravity as start--> <TextView android:id="@+id/view_1" android:text="GeeksforGeeks" android:background="@color/grey" android:textColor="@color/white" android:gravity="center" android:layout_gravity="start" android:padding="32dp" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="wrap_content" /> <!--sub view 2--> <!--Set the layout gravity as center--> <TextView android:id="@+id/view_2" android:text="GeeksforGeeks" android:background="@color/grey" android:textColor="@color/white" android:gravity="center" android:layout_gravity="center" android:padding="32dp" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="wrap_content" /> <!--sub view 3--> <!--Set the layout gravity as end--> <TextView android:id="@+id/view_3" android:text="GeeksforGeeks" android:background="@color/grey" android:textColor="@color/white" android:gravity="center" android:layout_gravity="end" android:padding="32dp" android:layout_width="wrap_content" android:layout_margin="32dp" android:layout_height="wrap_content" /> </LinearLayout> Design UI: LinearLayout and its Important Attributes with Examples in Android Comment K kartiksrivastava6 Follow 26 Improve K kartiksrivastava6 Follow 26 Improve Article Tags : Android Kotlin Android Java-Android Explore Android Tutorial 10 min read BasicsIntroduction to Android Development 5 min read History of Android 15+ min read Best Way to Become Android Developer â A Complete Roadmap 7 min read Android Development Prerequisites [2025] - Things to Learn Before Android Development 8 min read Android App Development Fundamentals for Beginners 6 min read Android Architecture 5 min read Android System Architecture 3 min read Android Boot Process 4 min read Difference between Java and Kotlin in Android with Examples 3 min read Interesting Facts About Android 3 min read Software Setup and ConfigurationDownload and Instal JDK on Windows, Mac and Linux 7 min read Guide to Install and Setup IntelliJ IDEA for Android App Development 5 min read Guide to Install and Setup Visual Studio for Android App Development 4 min read How to Run the Android App on a Real Device? 2 min read Resolving frequently occurring errors in Android Development 3 min read Android Studio Tutorial 9 min read File Structure & ComponentsComponents of an Android Application 3 min read Introduction to Activities in Android 6 min read Services in Android with Example 10 min read Core TopicsHow Does Android App Work? 7 min read Activity Lifecycle in Android with Demo App 9 min read Introduction to Gradle 4 min read What is Context in Android? 9 min read Bundle in Android with Example 6 min read Activity State Changes In Android with Example 6 min read Processes and Application Lifecycle in Android 7 min read Desugaring in Android 4 min read Difference Between AndroidX and Android Support Libraries 3 min read Memory Leaks in Android 7 min read Layout & ViewLayouts in Android UI Design 3 min read Android UI Layouts 5 min read LinearLayout and its Important Attributes with Examples in Android 3 min read Android LinearLayout in Kotlin 2 min read Android RelativeLayout in Kotlin 4 min read ConstraintLayout in Android 6 min read TextView widget in Android with Examples 5 min read TextView in Kotlin 3 min read Working With the TextView in Android 7 min read Autosizing TextView in Android 6 min read ButtonButton in Android 3 min read How to Add Radio Buttons in an Android Application? 5 min read RadioButton in Kotlin 4 min read How to add Toggle Button in an Android Application 3 min read ToggleButton in Kotlin 2 min read RadioGroup in Kotlin 3 min read Intent and Intent FiltersWhat is Intent in Android? 4 min read Implicit and Explicit Intents in Android with Examples 6 min read How to Send Data From One Activity to Second Activity in Android? 7 min read How to open dialer in Android through Intent? 3 min read Creating Multiple Screen Applications in Android 6 min read How to Open Camera Through Intent and Display Captured Image in Android? 6 min read Toast & RecyclerViewToasts for Android Studio 2 min read What is Toast and How to Use it in Android with Examples? 6 min read Android Toast in Kotlin 3 min read How to Change Toast font in Android? 3 min read How to add a custom styled Toast in Android 4 min read RecyclerView in Android with Example 7 min read Android | Horizontal RecyclerView with Examples 4 min read How to create a nested RecyclerView in Android 5 min read How to Create RecyclerView with Multiple ViewType in Android? 6 min read RecyclerView using ListView in Android With Example 5 min read Like