Tuesday, 24 March 2020

How to create Dynamic TextView in Android using Kotlin. | Kotlin Dynamic Textview

MainActivity.kt:-

val dynamicTextView = TextView(this)
dynamicTextView.text = "This is dynamically created textView"
dynamicTextView.textSize = 20f
relLayout.addView(dynamicTextView)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="This is xml generated textview" />

        <Button
            android:id="@+id/buttonClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="Click Me" />
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Find the GitHub Path:-

OUTPUT:-


No comments:

Post a Comment

how to change status bar color in jectpack compose.

 Step1: Go to MainActivity and do following: enableEdgeToEdge ( statusBarStyle = SystemBarStyle .light( Color . Green .h...