android kotlin - ConstraintLayout set margin programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout


class MainActivity : AppCompatActivity() {
    private lateinit var context: Context

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

        // Get the context
        context = this;

        // Get the widgets from XML layout
        val button = findViewById<Button>(R.id.button);
        val textView =  findViewById<TextView>(R.id.textView);

        // Text for text view
        val string = "Lorem Ipsum is simply dummy text of the printing" +
                " and typesetting industry. Lorem Ipsum has been the" +
                " industry's standard dummy text ever since the 1500s," +
                " when an unknown printer took a galley of type and" +
                " scrambled it to make a type specimen book."

        // Set the text view text
        textView.text = string

        // Set a button click listener
        button.setOnClickListener {
            // Get the text view layout parameters
            val layoutParams = textView.layoutParams
                    as ConstraintLayout.LayoutParams

            // Use this way if any side margin is already
            // defined in xml layout
            layoutParams.topMargin = 24.toDp(context)
            layoutParams.marginStart = 16.toDp(context)
            layoutParams.marginEnd = 34.toDp(context)
            layoutParams.bottomMargin = 8.toDp(context)

            // Another way to set margin for all sides
            /*layoutParams.setMargins(
                30.toDp(context), // left
                24.toDp(context), // top
                16.toDp(context), // right
                28.toDp(context) // bottom
            )*/

            // Finally, apply the layout params to text view
            textView.layoutParams = layoutParams
        }
    }
}


// Extension method to convert dp to equivalent pixels
fun Int.toDp(context: Context):Int = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    context.resources.displayMetrics
).toInt()
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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#FAEBD7"
        android:fontFamily="monospace"
        android:padding="12dp"
        android:textColor="#E52B50"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Set TextView  Margin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>