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: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: 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 30 second displayed.
activity_main.xml:
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout 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 = "To Display a text for 10 seconds"
android:id = "@+id/btn" />
</ LinearLayout >
|
Step 4: 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:
package com.example.gfg
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 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()
// Display initial text and set a delayed action
// to update text after 10 seconds
handler.postDelayed({
updateTextView( "Text to display for 10 seconds" )
// 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: