How to Display a Text in a Specific Time in Android using Handler? Last Updated : 24 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 ImplementationStep 1: Create a New ProjectTo 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.xmlNavigate 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 fileGo 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: Comment More infoAdvertise with us Next Article How to Display a Text in a Specific Time in Android using Handler? chinmaya121221 Follow Improve Article Tags : Android Kotlin Android Android-Misc Similar Reads Display Toast For a Specific Time in Android A Toast in Android is a message that appears on the screen for a specific time whenever invoked. This message appears at the bottom of the application leaving some margin at the bottom. In general, a Toast can be displayed for either 2 seconds (Toast.LENGTH_SHORT) or 3.5 seconds (Toast.LENGTH_LONG). 3 min read How to Search an Item in ListView using EditText and TextWatcher in Android? Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in 3 min read How to add a custom styled Toast in Android using Kotlin A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification that is used to display information when we perform any operation in the app. In this article, let's learn how to create a custom toast in Android using Kotlin.To cr 4 min read How to Build a Simple Torch App in Android using Kotlin? Torch Application is a very basic application that every beginner level android developer should definitely try to build while learning Android. In this article, we will be creating an application in which we will simply display a toggle button to switch on and switch off the torch. Note: If you are 4 min read How to change the Text Color of a Substring in android using SpannableString class? In this article, we will learn about how to change the text color of a substring of a string. It is easy to change the color of the whole string but to change the color of a substring we have to use a special class SpannableString. But SpannableString class is not really helpful when it comes to cha 2 min read How to Build a Simple Reflex Game in Android? A reflex game is a simple fun game that measures your responding speed. It is quite simple to make and understand. We will be designing a reflex game that will calculate your responding speed. The rules are simple just press the stop button when you see the change in background color, and the time y 4 min read How to Print Specific date-time in Golang? Golang supports time formatting and parsing via pattern-based layouts. In Go, the current time can be determined by using time.Now(), provided by the time package. Package time provides functionality for measuring and displaying the time. To print Current date-time you need to import the "time" pack 2 min read How to Get Current Time and Date in Android using Jetpack Compose? Many times in android applications we have to capture the current date and time within our android application so that we can update our data according to that. In this article, we will look at How to get the Current Time and Date in our android application using Jetpack Compose. Step by Step Imple 4 min read How to Build a Simple Alarm Setter App in Android? In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu 5 min read How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article, 4 min read Like