Dynamic TextView in Kotlin - GeeksforGeeks

18 Feb, 2021
Android TextView is an user interface that is used to display some text to the user.
In this article we will be discussing how to programmatically create a TextView in Kotlin .
Let’s start by first creating a project in Android Studio. To do so, follow these instructions:
Click on File, then New and then New Project and give name whatever you like
Then, select Kotlin language Support and click next button.
Select minimum SDK, whatever you need.
Select Empty activity and then click finish.
Second step is to design our layout page. Here, we will use the RelativeLayout to get the TextView from the Kotlin file.
<?xml version="1.0" encoding="utf-8"?><RelativeLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/root"        tools:context=".MainActivity">    </RelativeLayout>
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, we declare a variable TextView to create the TextView widget like this:
     val textView = TextView(this)
     //setting height and width
    textView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

then add the widget in layout using this:
 
        // Add TextView to LinearLayout
        layout?.addView(textView)

package com.geeksforgeeks.myfirstKotlinapp  import android.graphics.Colorimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.util.TypedValueimport android.view.ViewGroupimport android.widget.RelativeLayoutimport android.widget.TextViewimport android.widget.Toast  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?)    {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val layout = findViewById<RelativeLayout>(R.id.root)          // Create TextView programmatically.        val textView = TextView(this)          // setting height and width        textView.layoutParams= RelativeLayout.LayoutParams(            ViewGroup.LayoutParams.WRAP_CONTENT,             ViewGroup.LayoutParams.WRAP_CONTENT)        // setting text        textView.setText("GEEKSFORGEEKS")        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40f)        textView.setTextColor(Color.MAGENTA)        // onClick the text a message will be displayed "HELLO GEEK"        textView.setOnClickListener()        {            Toast.makeText(this@MainActivity, "HELLO GEEK",                 Toast.LENGTH_LONG).show()        }          // Add TextView to LinearLayout        layout ?.addView(textView)    }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="i.apps.textview">      <application            android:allowBackup="true"            android:icon="@mipmap/ic_launcher"            android:label="@string/app_name"            android:roundIcon="@mipmap/ic_launcher_round"            android:supportsRtl="true"            android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                  <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application>  </manifest>
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Resource Raw Folder in Android Studio
Broadcast Receiver in Android With Example
Services in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Kotlin Array
Android UI Layouts
Services in Android with Example
Android RecyclerView in Kotlin