Android LinearLayout Tutorial with Example

Android LinearLayout is used for application UI. It’s a view group that aligns and contain nested widget or layout in a single direction, Vertically or horizontally.

You have to specify the layout orientation with the android:orientation attribute. If you didn’t add orientation then default Horizontal will works.

Android LinearLayout Tutorial With Examples In Android kotlin

Types Of Linear Layout Orientation

There are two types of linear layout orientation:

  • Vertical
  • Horizontal

Note: For better performance , you should build your layout with new ConstraintLayout.

In the LinearLayout you can put all layout – RelativeLayout, FrameLayout, LinearLayout and TableLayout as nested layout. Next, inside the Linear layout, you have to add a widget as your app UI like – TextView, EditText, Button, ImageView, ListView, RecyclerView, etc.


Let’s build simple examples of Android LinearLayouts

We will see one by one example of how to use the different property of Linear Layout.


1.  Horizontal LinearLayout


Android LinearLayout Horizontal orientation


All nested view or widgets will arrange side by side in a Horizontal way. This code has 3 buttons in Horizontal orientation.


2.  Vertical LinearLayout


Android LinearLayout vertical orientation example


In a Vertical orientation, elements will come one after one. Below code have 3 buttons with Vertical orientation.


Layout Weight in Android 

Layout Weight is an important attribute for app UI design, some time you want to show fill full-screen width or height of app. But the problem is that you don’t know what the size and pixel device is running your app? So you can do it with responsive design, where layout Weight helps to achieve it.


For a Horizontal Layout

And for Horizontal child same amount of space in screen, set the android:layout_width of each view to "0dp" . Then set the android:layout_weight of each view to "1".


Android LinearLayout Horizontal orientation Weight



For a Vertical Layout

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" . Then set the android:layout_weight of each view to "1".

Android LinearLayout vertical orientation example weight



Complete Example of Android LinearLayout :

In this example, we are using nested linear layout and widgets.

Step 1. Create new project “Build Your First Android App in Kotlin

Step 2. Add following code in  activity_main.xml resource file

LinearLayout in LinearLayout in widgets

Step 3. Set resource layout file in “MainActivity.kt”

Step 4. Run the application, in an emulator or On your Android device.

Output screenshot Android LinearLayout example :

Output screenshot Android Linear Layout example


Download source code Android Linear layout in kotlin


Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)



Coding in Kotlin


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.