The Wayback Machine - https://web.archive.org/web/20240930184533/https://www.geeksforgeeks.org/android-linearlayout-in-kotlin/
Open In App

Android LinearLayout in Kotlin

Last Updated : 05 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 the 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.

Can illustrate the Horizontal and Vertical List with an Image.

Linear_Layout

In this article, we are going to discuss the declaration and implementation of LinearLayout.

How to Declare LinearLayout in an XML file?

First of all, we should declare the LinearLayout in the 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>

LinearLayout in activity_main.xml file

Following is the code for LinearLayout in xml file.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Main_View"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtVw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_marginTop="20dp"
        android:paddingTop="20dp"
        android:text="Enter your name here:"
        android:textSize="24dp" />

    <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:backgroundTint="@color/colorPrimary"
        android:text="show"
        android:textColor="@android:color/white" />



</LinearLayout>

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.

MainActivity.kt
package com.example.text_view_application

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.text_view_application.ui.theme.Text_View_ApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        val showButton:Button = findViewById(R.id.showInput)

        val editText:EditText = findViewById(R.id.editText)

        val textView:TextView = findViewById(R.id.txtVw)
    }
}

LinearLayout Output:

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

LinearLayout_Output

Previous Article
Next Article

Similar Reads

LinearLayout and its Important Attributes with Examples in Android
LinearLayout is the most basic layout in android studio, that aligns all the children sequentially either in a horizontal manner or a 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 and If you apply android:orientatio
3 min read
Difference Between LinearLayout and RelativeLayout in Android
LinearLayout LinearLayout is a type of view group which is responsible for holding views in it either Horizontally or vertically. It is a type of Layout where one can arrange groups either Horizontally or Vertically. Example Diagram: Syntax: C/C++ Code <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_c
3 min read
Creating a Anagram Checker Android App in Android Studio with Kotlin
An anagram of a word is another word that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. In this article, we will be building an Anagram Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered words are anagrams of e
3 min read
Android Shared Element Transition with Kotlin
Shared element transition is seen in most of the applications which are used when the user navigates between two screens. This type of animation is seen when a user clicks on the items present in the list and navigates to a different screen. During these transitions, the animation which is displayed is called a shared element transition. In this ar
4 min read
Expandable RecyclerView in Android with Kotlin
In this article, we will get to know how to implement the expandable RecyclerView. In General, we will show the list of items in the listview but in some situations like if you want to show the sub-list items, then we have to use an expandable list. See the below image for a better understanding. Step by Step Implementation 1. If you observe the ab
7 min read
A Complete Guide to Learn Kotlin For Android App Development
Kotlin is a statically typed, cross-platform, general-purpose programming language for JVM developed by JetBrains. This is a language with type inference and fully interoperable with Java. Kotlin is concise and expressive programming as it reduces the boilerplate code. Since Google I/O 2019, Android development has been Kotlin-first. Kotlin is seam
8 min read
Android - Save ArrayList to SharedPreferences with Kotlin
SharedPreferences is local storage in android which is used to store data in the form of key and value pairs within the android devices. We can store data in the form of key and value pairs using Shared Preferences. In this article, we will take a look at How to Save Array List to SharedPreferences in Android using Kotlin. A sample video is given b
9 min read
Create an Android App that Displays List of All Trending Kotlin git-hub Repositories
Kotlin is a popular programming language that has gained much attention in recent years. It is known for its simplicity, conciseness, and compatibility with existing Java code. GitHub is a popular platform for hosting and sharing code, including Kotlin projects. This article will discuss how to build an application that shows a list of Kotlin GitHu
12 min read
Android - Line Graph View with Kotlin
Graphs are used in android applications to display vast amounts of data in an easily readable form. There are different types of graphs used such as BarGraph, GroupBar graph, point, and line graph to represent data. In this article, we will take a look at How to use Line Graph View in Android using Kotlin. A sample video is given below to get an id
3 min read
Android BottomSheet Example in Kotlin
The Bottom Sheet is seen in many of the applications such as Google Drive, Google Maps and most of the applications used the Bottom Sheet to display the data inside the application. In this article, we will take a look at implementing a Bottom Sheet in the Android app using Kotlin in Android Studio. What is Bottom Sheet? Bottom Sheet is a component
5 min read
Android EditText in Kotlin
EditText is used to get input from the user. EditText is commonly used in forms and login or registration screens. Following steps are used to create EditText in Kotlin: Add a EditText in activity_main.xml file.Add a Button in activity_main.xml file.Open MainActivity.kt file and set OnClickListener for the button to get the user input from EditText
3 min read
Android Fade In/Out in Kotlin
In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking. Fade In and Fade out animations are used to modify the appearance of any view over a set interval of time so that user can be notified about the changes that are occurring in our application. In this article we will be discussin
3 min read
Android Slide Up/Down in Kotlin
In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking. In this article we will be discussing how to create a Slide Up/Down animation in Kotlin. XML Attributes Description android:duration It is used to specify the duration of animation android:fromYDelta It is the change in Y coordi
3 min read
Android Animations in Kotlin
Animation is a method in which a collection of images are combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. Android has quite a few tools to help you create animations with relative ease. so in this article we will learn to create animations using Kotlin. below a
5 min read
Android progress notifications in Kotlin
In this tutorial you'll learn how to create a basic Progress Notification (Indeterminate progress indicator and Fixed-duration progress indicator) for Android using Kotlin. Before we begin, let us first understand the components of a Notification in Android. [Tex]\\*[/Tex] [Tex]\\*[/Tex] Small Icon - Required, can be set with setSmallIcon(). Applic
3 min read
Android - RecyclerView as Staggered Grid with Kotlin
Staggered Grid View has been seen in most applications such as Pinterest in which each item of grid view takes its own height and aligns within the grid view according to that. In this article, we will look at how to implement Staggered Grid Layout Manager to our Recycler View in Android. Note: If you are looking to implement Staggered Grid Layout
5 min read
Difference between Java and Kotlin in Android with Examples
Kotlin KOTLIN is a cross platform, statically types, general purpose programming language with type inference. KOTLIN is designed to interoperate fully with java but type inference allows its syntax to be more concise.KOTLIN is sponsored by JetBrains and Google through the Kotlin Foundation. Java JAVA is an Object Oriented Programming Language deve
3 min read
Thread Priority in Kotlin & Android
Every thread in a process has a Priority. They are in the range of 1 to 10. Threads are scheduled according to their priorities with the help of a Thread Scheduler. There can be 3 priority constant set for a Thread which are: MIN_PRIORITY which equals to 1 MAX_PRIORITY which equals to 10 NORM_PRIORITY which is a default value and equals to 5 Below
2 min read
Android - Extract Data From JSON Array using Volley Library with Kotlin
JSON responses are of 2 types i.e. JSON Object and JSON Array. JSON Array consists of individual JSON objects which are having same similar structure but have different values within it. JSON Array is used to parse the data in the form of a list or an array. In the previous article, we have taken a look at How to extract data from JSON Object in An
7 min read
Kotlin Coroutines on Android
Asynchronous programming is very important and it's now a common part of modern application. It increases the amount of work that your app can perform in parallel. This allows running heavy tasks away from UI Thread in the background, which ultimately gives a smooth and better experience to the user of the app. Coroutine in Kotlin The Kotlin team d
5 min read
How to Create a Dark Mode for a Custom Android App in Kotlin?
The dark mode is a feature that allows you to switch the color theme of an app or a whole OS to black or something on the brink of it. Beyond the joys of invigorating a tired design, one may wish to choose because it makes watching the device screen way more comfortable and relaxing on the eyes. Typical pixel values during the regular mode fall bet
3 min read
Android Pull to Refresh with RecyclerView in Kotlin
Pull to Refresh is used to update the data within the list in our android application. For implementing this we have to use Swipe to Refresh Layout. Using this widget when the user swipes down the list which is being displayed on the screen is updated. In this article, we will be building a simple application in which we will be displaying a list v
6 min read
Generate PDF File in Android using Kotlin
Most of the applications provide users with the facility of bills to be downloaded from the mobile applications. This type of application gets the data from the APIS or data within the application and this data is used to generate the PDF files. PDF files within android are generated using canvas. In this article, we will take a look at How to gene
7 min read
How to Convert Java Code to Kotlin Code in Android Studio?
In Google I/O 2017, Kotlin has been declared as an official language for Android app development. This language gains popularity among developers very quickly because of its similarities as well as interoperable with Java language. One can mix code of Java and Kotlin while designing an Android project. Some of the major challenges faced by develope
4 min read
How to Convert Kotlin Code to Java Code in Android Studio?
Java programming language is the oldest and most preferred language for Android app development. However, during Google I/O 2017, Kotlin has been declared as an official language for Android development by the Google Android Team. Kotlin has gained popularity among developers very quickly because of its similarities as well as interoperable with th
4 min read
Android - Build Image Filters Like Instagram using Kotlin
Many social media apps nowadays provide a feature within their application to edit photos by applying some beautiful filters to them and making them more beautiful. We can get to see these types of filters on Instagram and Snapchat. In this article, we will be building an application in which we will be adding Instagram Image filters to our image o
7 min read
How to Close or Hide Android Soft Keyboard with Kotlin?
Many times there is a need in which we have to close the android soft keyboard programmatically when the user has typed some text within the edit text. This type of functionality is generally required in four digits pins in which after users type 4 digit pin the keyboard will be closed programmatically within the android application. We will be cre
3 min read
How to Create Your Own Custom View in Android with Kotlin?
In this article, we're going to talk about how we can create our own custom view in Android step by step. We all know that in the beginning android platform provides us some basic views for example - TextView, ImageView, EditText, Button, ImageButton, RadioButton, etc. But, sometimes we need a whole new type of view where our prebuilt layouts and w
4 min read
Kotlin Flow in Android with Example
Kotlin Flow is one of the latest addition to the Kotlin Coroutines. With Kotlin Flow we can handle streams of data asynchronously which is being executed sequentially. What are we going to build in this article? We will build a simple app that fetches some data from API and shows it on the screen. It's a simple app demonstrating the Kotlin flow wor
7 min read
Generate QR Code in Android using Kotlin
Many applications nowadays use QR codes within their application to hide some information. QR codes are seen within many payment applications which are used by the users to scan and make payments. The QR codes which are seen within these applications are generated inside the application itself. In this article, we will take a look at How to generat
4 min read