Skip to content

Instantly share code, notes, and snippets.

@le0nidas

le0nidas/debounce_timer.kt Secret

Created Mar 14, 2021
Embed
What would you like to do?
class MainActivity : AppCompatActivity() {
private var timer = Timer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bindings = ActivityMainBinding.inflate(layoutInflater)
setContentView(bindings.root)
with(bindings) {
userInput.doAfterTextChanged { text ->
timer.cancel() // cancel any previous delay
timer = Timer() // schedule a new one
timer.schedule(500L) {
// we are in a background thread here
userInput.handler.post { userResult.text = text }
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment