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>
No comments:
Post a Comment