Hello Friends,
In this tutorial I am going create a textview dynamically in kotlin and add it to Linearlayout layout.
1. activity_main.xml: Following is the activity_main.xml containing the TextView with the text .
3. Finally the complete code is:
TextViewSample.kt
In this tutorial I am going create a textview dynamically in kotlin and add it to Linearlayout layout.
1. activity_main.xml: Following is the activity_main.xml containing the TextView with the text .
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="https://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=".TextViewSample"> <LinearLayout android:id="@+id/ll_main_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_static" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="20sp" android:justificationMode="inter_word" android:text="This textview is created from xml"/> </LinearLayout> </android.support.constraint.ConstraintLayout>2. Creation of textview dynamically,
val tv_programtically = TextView(this) tv_programtically.textSize = 20f tv_programtically.text = "This textview is an dynamic textview in kotlin"
3. Finally the complete code is:
TextViewSample.kt
package com.android.developer.kotlinsample import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.LinearLayout import android.widget.TextView class TextViewSample : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_text_view_sample) /** Dynamic creation of text view */ val tv_programtically = TextView(this) tv_programtically.textSize = 20f tv_programtically.text = "This textview is an dynamic textview in kotlin" /**------end--------**/ // add TextView to LinearLayout val ll_main_layout = findViewById(R.id.ll_main_layout) as LinearLayout ll_main_layout.addView(tv_programtically) } }Hope this will help some one. Enjoy Coding.... :)
0 comments:
Post a Comment