TextView OnClickListener Kotlin using Android studio
Hello, reader in the article we will learn how to set OnClickListener for Texiview using kotlin language. TextView widget use to display text to users in android and TextView is also non-clickable. follow the below article on how to set the part of the Android text view as clickable in Kotlin
in HTML clickable links is a very common technique use to send further information. in this post, I will give you an example of how to make clickable text in android using kotlin language.
TextView OnClickListener Kotlin
quick look into the below code to set Clickable textview in kotlin program using android studio.
// get reference to textview
val textView = findViewById<TextView>(R.id.text_view_id) as TextView
// set on-click listener
textView?.setOnClickListener
{
}
if you want to set Textview OnClickListener then first create new project with kotlin support in android studio and replace xml and kotlin code with below content.
You may know also:
WebView in an Android App using Kotlin language
Radio Button Example in kotlin
Android Calculator in Kotlin Using Android Studio
First open activity_main.xml file and replace the code given below:
activity_main.xml
<androidx.constraintlayout.widget.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">
<TextView
android:id="@+id/text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textColor="#3F51B5"
android:textSize="40dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now add the following code to MainActivity.kt file.
MainActivity.kt
import androidx.appcompat.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)
val textView = findViewById<TextView>(R.id.text_view_id) as TextView
textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
R.string.text_on_click, Toast.LENGTH_LONG).show() }
}
}
Let’s try to run your application in an emulator or on your android device.
Output screenshot TextView OnClickListener Kotlin using Android studio example:

