Skip to main content

How to use Looper in Kotlin Android with a Handler

How to use Looper in Kotlin Android with a Handler.

Here's a step-by-step tutorial on how to use Looper in Kotlin Android with a Handler:

Step 1: Create a new Kotlin Android project

Start by creating a new Kotlin Android project in Android Studio. Choose an appropriate project name and configure the project settings.

Step 2: Create a Handler object

In your MainActivity.kt file, create a Handler object to handle messages and runnables.

import android.os.Handler

class MainActivity : AppCompatActivity() {
private lateinit var handler: Handler

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

handler = Handler()
}
}

Step 3: Create a Looper object

Next, create a Looper object and associate it with the current thread. This will allow the thread to process messages and runnables sent to the Handler.

import android.os.Handler
import android.os.Looper

class MainActivity : AppCompatActivity() {
private lateinit var handler: Handler

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

handler = Handler(Looper.getMainLooper())
}
}

Step 4: Send messages and runnables to the Handler

Now that you have a Handler object with a Looper, you can send messages and runnables to it. Messages are objects that encapsulate a specific action, while runnables are blocks of code that can be executed.

import android.os.Handler
import android.os.Looper

class MainActivity : AppCompatActivity() {
private lateinit var handler: Handler

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

handler = Handler(Looper.getMainLooper())

// Send a message to the handler
handler.sendMessage(handler.obtainMessage(MESSAGE_CODE, "Hello from message!"))

// Post a runnable to the handler
handler.postDelayed({
// Code to be executed after a delay
}, DELAY_MS)
}

companion object {
private const val MESSAGE_CODE = 1
private const val DELAY_MS = 1000L
}
}

Step 5: Handle messages and runnables in the Handler

To handle messages and runnables sent to the Handler, override the handleMessage method of the Handler object.

import android.os.Handler
import android.os.Looper
import android.os.Message

class MainActivity : AppCompatActivity() {
private lateinit var handler: Handler

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

handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
when (msg.what) {
MESSAGE_CODE -> {
val message = msg.obj as String
// Handle the message
}
}
}
}

// Send a message to the handler
handler.sendMessage(handler.obtainMessage(MESSAGE_CODE, "Hello from message!"))

// Post a runnable to the handler
handler.postDelayed({
// Code to be executed after a delay
}, DELAY_MS)
}

companion object {
private const val MESSAGE_CODE = 1
private const val DELAY_MS = 1000L
}
}

That's it! You have now successfully used Looper in Kotlin Android with a Handler. You can use the Handler to send messages and runnables to be processed on the main thread or any other thread associated with a Looper.