Open In App
Related Articles

Android LinearLayout in Kotlin

Like Article
Save Article
Report issue
Android LinearLayout is a ViewGroup subclass, used to provide child View elements one by one either in a particular direction either horizontally or vertically based on the orientation property. We can specify the linear layout orientation using android:orientation attribute. All the child elements arranged one by one in multiple rows and multiple columns.
  1. Horizontal list: One row, multiple columns.
  2. Vertical list: One column, multiple rows.
In this article, we are going to discuss declaration and implementation of LinearLayout.

How to declare LinearLayout in XML file?

First of all, we should declare the LinearLayout in layout file using the below code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://"
    android:orientation="vertical" >
    // Add another child elements here like
    // EditText, button etc


LinearLayout in activity_main.xml file

Following is the code for LinearLayout in xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Enter your name here:"


MainActivity.kt file

When we have created layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element form the XML using findViewById.


package com.geeksforgeeks.myfirstkotlinapp
    import import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?)
            // finding the UI elements
            val showButton
            = findViewById<Button>(
                val editText
            = findViewById<EditText>(
                val textView
            = findViewById<TextView>(


LinearLayout Output:

We can run the application using the Android Virtual Device(AVD) to get the output of the above code.

Last Updated : 16 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads