Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Created August 1, 2019 10:28
Show Gist options
  • Save EmmanuelGuther/4bda5a766aeb825f0de3fa3e005820a1 to your computer and use it in GitHub Desktop.
Save EmmanuelGuther/4bda5a766aeb825f0de3fa3e005820a1 to your computer and use it in GitHub Desktop.
A kotlin extension
fun TextView.afterTextChangedDelayed(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
var timer: CountDownTimer? = null
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun afterTextChanged(editable: Editable?) {
timer?.cancel()
timer = object : CountDownTimer(1000, 1500) {
override fun onTick(millisUntilFinished: Long) {}
override fun onFinish() {
afterTextChanged.invoke(editable.toString())
}
}.start()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment