Android LinearLayout in Kotlin - GeeksforGeeks

16 Mar, 2022
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.
Horizontal list: One row, multiple columns.Vertical list: One column, multiple rows.
Horizontal list: One row, multiple columns.
Vertical list: One column, multiple rows.
In this article, we are going to discuss declaration and implementation of LinearLayout.
First of all, we should declare the LinearLayout in layout file using the below code.
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >         // Add another child elements here like    // EditText, button etc  </LinearLayout>
Following is the code for LinearLayout in xml file.
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:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="16dp"        android:text="Enter your name here:"        android:textSize="24dp"        android:id="@+id/txtVw"/>      <EditText        android:id="@+id/editText"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="16dp"        android:hint="Name"        android:inputType="text"/>      <Button        android:id="@+id/showInput"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="show"        android:backgroundTint="@color/colorPrimary"        android:textColor="@android:color/white"/>  </LinearLayout>
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.
Kotlin
package com.geeksforgeeks.myfirstkotlinapp      import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView      class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?)    {        super.onCreate(savedInstanceState)            setContentView(R.layout.activity_main)            // finding the UI elements            val showButton            = findViewById<Button>(R.id.showInput)                val editText            = findViewById<EditText>(R.id.editText)                val textView            = findViewById<TextView>(R.id.txtVw)    }}
We can run the application using the Android Virtual Device(AVD) to get the output of the above code.
Akanksha_Rai
ayushpandey3july
Kotlin Android
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Retrofit with Kotlin Coroutine in Android
How to Add and Customize Back Button of Action Bar in Android?
How to Get Current Location in Android?
Kotlin Setters and Getters
How to Change the Color of Status Bar in an Android App?
Kotlin Android Tutorial
Kotlin when expression