Android LinearLayout in Kotlin
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.
- Horizontal list: One row, multiple columns.
- Vertical list: One column, multiple rows.
Can illustrate the Horizontal and Vertical List with an Image.
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 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.
<?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.
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.
Contact Us