How to Display a Text in a Specific Time in Android using Handler?
Last Updated :
24 May, 2024
The Handler class in Android is a fundamental component that facilitates communication and synchronization between different threads, particularly between background threads and the main UI thread. It offers a powerful mechanism for executing code at specific times or after specific delays, enabling developers to manage the timing and sequencing of tasks in their applications.
In this article, we are going to see how can we display a text for a specific time period using a handler class in Android using Kotlin.
Step By Step Implementation
Step 1: Create a New Project
To Create a New project in Android Studio please refer to How to Create/Start a New Project in Android Studio and select the language as Kotlin.
Step 2: Working with activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to that file.

Below is the code for the activity_main.xml file. This xml file represents our app UI, our UI contains one button by clicking it the textview for 10 second displayed.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/black"
android:textStyle="bold"
android:padding="10dp"
android:textSize="20dp"
android:id="@+id/textview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button"
android:id="@+id/btn"/>
</LinearLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt and follow the below code. Below is the code for the MainActivity.kt. Comments are added inside the code to understand the code in more detail. This activity contains the main logic to display a textview for 30 seconds using hanlder.
MainActivity.kt:
MainActivity.kt
package com.example.handler_time
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import java.util.*
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
lateinit var button: Button
private lateinit var handler: Handler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize views
textView = findViewById(R.id.textview)
button = findViewById(R.id.btn)
// Set a click listener for the button
button.setOnClickListener {
// Initialize the handler on button click
handler = Handler(Looper.getMainLooper())
// Get the current time
val currentTime = System.currentTimeMillis()
val message = "Clicked the button wait 10 sec"
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
// Display initial text and set a delayed action
// to update text after 10 seconds
handler.postDelayed({
updateTextView("This is a testing application")
// Set another delayed action to update text
// after 10 seconds to show time has ended
handler.postDelayed({updateTextView("Time has ended")}, 10 * 1000)
// 10 seconds in milliseconds
}, 0)
}
}
// Function to update the TextView
// with the provided text
private fun updateTextView(text: String) {
textView.text = text
}
}
Output: