Kotlin: How to delay code in Android without making the UI freeze

kotlin wait inside coroutine
kotlin coroutines timer
delay in kotlin
thread sleep vs delay
kotlin pause function
kotlin thread
pause coroutine kotlin
delay vs sleep

I am trying to delay code in Kotlin I have tried


But its freezes the UI.

Does somebody know why this is happening And how to delay without freezing the UI?

Android sleep() without blocking UI, The right way to do this is to post a Runnable to a Handler. Then put whatever code you want to run after the delay in the run() method of the  Kotlin React Native Non freezing sleep in android app That means if you were to call <code>Thread.sleep()</code> on the UI <code>Thread</code> the UI would

You can use Handler object https://developer.android.com/reference/android/os/Handler.

val handler = Handler()
val runnable = Runnable {
    // code which will be delayed  

handler.postDelayed(runnable, 1000)

1000 is time in miliseconds, you should replace it with your value.

Kotlin: How to delay code in Android without making the UI freeze, I am trying to delay code in Kotlin I have triedThread.sleep(1000)But its freezes the UI.Does somebody know why this is happeningAnd how to  Please add that it is only available on android, since the question asks for a general kotlin method (although it does have the Android tag) – Yoav Sternberg Apr 20 '17 at 13:43 5 It's not constructive from your side.

Thread.sleep and coroutine delay, Is there any particular advantage of Coroutine delay over Thread.sleep? The question is not actually about kotlin since you are referring to core java methods. As for your code, you are trying to use blocking sleep method inside the coroutine which If you call it on UI, you will freeze UI thread and the block display loop. Kotlin coroutines are a relatively new multitasking management method that allows you to perform long-running tasks on any thread, including Android’s main UI thread, without causing your app to


Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, [...]

see https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)

If you call this on the UI thread, which is the default thread your code runs on, it will stop and wait.

In pure Kotlin you should use a coroutine:

import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch { // launch new coroutine in background and continue
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    println("Hello,") // main thread continues while coroutine is delayed
    Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive

If you are programming an Android app you can use a Handler and Androidx for especially nice syntax:

Handler().postDelayed( 1000 ) { doSomething() }

Better performance through threading, This main thread, also known as the UI thread, is responsible for everything that In addition, app can explicitly enqueue blocks on their own, without using the framework. Nearly any block of code your app executes is tied to an event as a non-static inner class of some activity (or an inner class in Kotlin). kotlin. Score 0. Posts 1. 8 Kotlin: How to delay code in Android without making the UI freeze Jan -2 moving a 2d object from point a to b without changing its


this is the code part you asked for. But for your solution, first understand




Similarly you should understand other values of


Writing Asynchronous Code for Android, Writing asynchronous code is hard, even when we have amazing in Kotlin and on Android, where the features of Java 8 have been a freezing its execution for as long as we request, while delay() will For coroutines to be suspendable without blocking everything else, we interface NetworkService { Kotlin Coroutines - Use Cases on Android 🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included! This repository is intended to be a "Playground Project". You can quickly look up and play around with the different Coroutine Android implementations.

How to Delay Code Execution in Java, It is relatively common for Java programs to add a delay or pause in This interface can schedule code to run once after a specified delay or at  In working with Kotlin on Android, I've needed to employ the same technique. Kotlin obviously doesn't have a setTimeout, but it does have coroutines to achieve approximately the same effect. To run an async coroutine with delay, you can use the following Kotlin code:

Use Kotlin Coroutines in your Android App - Codelabs, In this codelab you'll learn how to use Kotlin Coroutines in an Android app—a new way of managing background threads that can simplify code by reducing the​  However, the long-running CPU-consuming code might need to be executed in the context of Dispatchers.Default and UI-updating code might need to be executed in the context of Dispatchers.Main. Usually, withContext is used to change the context in the code using Kotlin coroutines, but code in the flow { } builder has to honor the context

Simplify asynchronous programming with Kotlin's , Kotlin coroutines are a relatively new multitasking management method that Android's main UI thread, without causing your app to freeze or crash, In the following code we're using Delay() to suspend the coroutine's  The direct reply action, introduced in Android 7.0 (API level 24), allows users to enter text directly into the notification, which is delivered to your app without opening an activity. For example, you can use a direct reply action to let users reply to text messages or update task lists from within the notification.

  • do the answers solve your problem? :)
  • Hi asaf, let me know if this helps.
  • Maybe worth starting by pointing out that the OP's code is running on the ‘main thread’ (does Android call that the Event-Dispatch Thread like Swing does?), and explaining why?
  • I tried the kotlin coroutine but its does a compile error is there a library that i should use?