Kotlin Android LinearLayout – Vertical Orientation
You can specify vertical orientation for a LinearLayout in XML layout file or set vertical orientation for the LinearLayout dynamically in Kotlin code.
In this tutorial, we will learn both the ways of setting vertical orientation for LinearLayout in layout XML file or in Kotlin file.
LinearLayout Vertical Orientation in XML Layout file
To define a LinearLayout with vertical orientation in XML layout file, assign the attribute orientation with the value "vertical"
.
Sample code snippet for LinearLayout with orientation=”vertical” is given below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<!-- child views -->
</LinearLayout>
Example
Let us create an Android Application with Kotlin support, and use the following code activity_main.xml layout file.
We have a LinearLayout with vertical orientation and five children. The children are text views.
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#FFFFFF"
android:text="Item 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#FFFFFF"
android:text="Item 4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 5" />
</LinearLayout>
MainActivity.kt
package com.kotlinandroid.myapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Screenshot
LinearLayout Vertical Orientation in Kotlin File
You can also define the orientation of LinearLayout dynamically to vertical in Kotlin File.
Sample code snippet for LinearLayout in XML file without orientation specified is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<!-- child views -->
</LinearLayout>
Then in Kotlin file, access the LinearLayout using findViewById() and assign vertical orientation to the LinearLayout.
var ll = findViewById<LinearLayout>(R.id.linearlayout_items)
ll.orientation = LinearLayout.VERTICAL
Example Android Application
Let us create an Android Application with Kotlin support, and use the following code activity_main.xml layout file.
We have a LinearLayout in XML file with five children and no orientation specified.
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:id="@+id/linearlayout_items"
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:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#FFFFFF"
android:text="Item 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#FFFFFF"
android:text="Item 4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25sp"
android:textSize="25sp"
android:background="#F5F5F5"
android:text="Item 5" />
</LinearLayout>
MainActivity.kt
package com.kotlinandroid.myapp
import android.os.Bundle
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var ll = findViewById<LinearLayout>(R.id.linearlayout_items)
ll.orientation = LinearLayout.VERTICAL
}
}