Kotlin For Android 1 : Android TextView

Today we will learn about Android TextView in Kotlin. This article includes 2 content :

  • New TextView programmatically
  • TextView OnClickListenner

1. New TextView programmatically
 In this article, we will create two TextView with two way:

  • Generate from XML
  • Creating a new TextView programmatically and add TextView to a LinearLayout in Kotlin Android

Firstly, we will create a new project Kotlin (You should use Android Studio lastest version — support Kotlin)

activity_main.xml : it will contain TextView : “This is TextView generate from xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://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=".MainActivity">

<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView generate from xml"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.kt : Create a TextView with text “This is TextView generate in Kotlin” and add it to the LinearLayout.

package com.tvsoft.kotlintutorial

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val llMain = findViewById<LinearLayout>(R.id.ll_main_layout)

// creating dynamic textview
val tvDynamic = TextView(this)
tvDynamic.textSize = 20f
tvDynamic.text = "This is TextView generate in Kotlin"
// add view to layout
llMain.addView(tvDynamic)
}
}

And result

2. TextView OnClickListenner

Create a new project Kotlin
 activity_main.xml: layout contain TextView with id.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://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=".MainActivity">

<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_click_me"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Click me"
android:gravity="center"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.kt : we can use the method setOnClickListenner() on the object of TextView

package com.tvsoft.kotlintutorial

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// get reference to TextView
val tvClickMe = findViewById<TextView>(R.id.tv_click_me)
// set onclicklistenner
tvClickMe.setOnClickListener {
// show toast
Toast.makeText(this,"Clicked on TextView 'Click me'", Toast.LENGTH_SHORT).show()
}
}
}

And output

Thank you!


Originally published at Code for fun.