Home » Android Kotlin – Linear Layout Example

Android Kotlin – Linear Layout Example

android kotlin linear layout example

In this post, you will get an Android LinearLayout example. LinearLayout in Android is one of the basic layouts. Linear Layout can be used when you want to display child View elements one after another either vertically or horizontally.

The orientation of linear layout can be specified using the android:orientation attribute. When you set android:orientation=“vertical”, all child View elements will be displayed one by one vertically as shown in the image below.

android vertical linearlayout

When android:orientation attribute is set to horizontal, all child View elements will be displayed one by one horizontally as shown in the below image.

android horizontal linearlayout

Android LinearLayout Example – Horizontal Orientation

Here in the below example, we have displayed three buttons that are inside LinearLayout and the orientation is set to horizontal.

activity_main.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">
    <Button
        android:id="@+id/buttonPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Phone"/>
    <Button
        android:id="@+id/buttonLaptop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Laptop"/>
    <Button
        android:id="@+id/buttonDesktop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Desktop"/>
</LinearLayout>

Output:

linear layout horizontal orientation

Vertical Orientation – Example

To display the same buttons vertically, just set android:orientation=”vertical”.

activity_main.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">
    <Button
        android:id="@+id/buttonPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Phone"/>
    <Button
        android:id="@+id/buttonLaptop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Laptop"/>
    <Button
        android:id="@+id/buttonDesktop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:text="Desktop"/>
</LinearLayout>

Output:

 

linear layout vertical orientation

Also Check:

Leave a Reply

Your email address will not be published.