. f Kotlin-Create TextView Programatically ~ Android Developers Blog

Wednesday, 5 December 2018

Kotlin-Create TextView Programatically

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 .

  1. <?xml version="1.0" encoding="utf-8"?>    
  2.  <android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"    
  3.   xmlns:app="http://schemas.android.com/apk/res-auto"    
  4.   xmlns:tools="http://schemas.android.com/tools"    
  5.   android:layout_width="match_parent"    
  6.   android:layout_height="match_parent"    
  7.   tools:context=".TextViewSample">    
  8.   <LinearLayout    
  9.    android:id="@+id/ll_main_layout"    
  10.    android:layout_width="match_parent"    
  11.    android:layout_height="wrap_content"    
  12.    android:orientation="vertical">    
  13.    <TextView    
  14.     android:id="@+id/tv_static"    
  15.     android:layout_width="wrap_content"    
  16.     android:layout_height="wrap_content"    
  17.     android:textSize="20sp"    
  18.     android:padding="20sp"    
  19.     android:justificationMode="inter_word"    
  20.     android:text="This textview is created from xml"/>    
  21.   </LinearLayout>    
  22.  </android.support.constraint.ConstraintLayout>  
2. Creation of textview dynamically,
  1. val tv_programtically = TextView(this)  
  2. tv_programtically.textSize = 20f  
  3. tv_programtically.text = "This textview is an dynamic textview in kotlin"  

3. Finally the complete code is:

  TextViewSample.kt
  1. package com.android.developer.kotlinsample  
  2.   
  3. import android.support.v7.app.AppCompatActivity  
  4. import android.os.Bundle  
  5. import android.widget.LinearLayout  
  6. import android.widget.TextView  
  7. class TextViewSample : AppCompatActivity() {  
  8.   
  9.   override fun onCreate(savedInstanceState: Bundle?) {  
  10.     super.onCreate(savedInstanceState)  
  11.     setContentView(R.layout.activity_text_view_sample)  
  12.   
  13.     /** Dynamic creation of text view */  
  14.     val tv_programtically = TextView(this)  
  15.     tv_programtically.textSize = 20f  
  16.     tv_programtically.text = "This textview is an dynamic textview in kotlin"  
  17.     /**------end--------**/  
  18.   
  19.     // add TextView to LinearLayout  
  20.     val ll_main_layout = findViewById(R.id.ll_main_layout) as LinearLayout  
  21.     ll_main_layout.addView(tv_programtically)  
  22.   }  
  23. }  
Hope this will help some one. Enjoy Coding.... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.