Home » Add View to LinearLayout Programmatically Android Kotlin

Add View to LinearLayout Programmatically Android Kotlin

add view linearlayout programmatically android

In this blog post, we are going to show you how to add a view to the Linear Layout programmatically and how to add a view at a specified position inside Linear Layout. In this tutorial, we will be using Kotlin programming language to demonstrate the example. 

Add View to Linear Layout Programmatically

Step1: Create a new Android project with an empty activity.

Step2: After Creating the new projects I wrote the following XML code in activity_main.xml.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
    <Button
        android:id="@+id/buttonAddView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#12947f"
        android:text="Add View"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

We have created a linear layout and a button labeled “Add View”. When this button is clicked, a button is added to that linear layout.

Step3: To add a button view to the linear layout when the Add View button is clicked, add the following code to MainActivity.kt.

MainActivity.kt

package com.example.addviewlinearlayout
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        buttonAddView.setOnClickListener {
            val button = Button(this)
            button.text = "This is Button"
            linear_layout.addView(button)
        }
    }
}

In the above code, we have defined an on-click listener to the Add View button. When this button is clicked, a new button is initialized and the same will be added to our linear layout.

adding view to linear layout android kotlin output

Add View to Linear Layout at a Specific Index Programmatically

If you want to add a view at a specific index (or position) inside a linear layout you can do it by just writing below the line of code.

linear_layout.addView(<strong>your_view</strong>, 2)

Here in the above code, your_view will be your view (TextView, ButtonView, etc), and the second parameter is index value or position. For example, I have given index value as 2, you can give your own value.

Leave a Reply

Your email address will not be published.