Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Created October 23, 2018 08:23
Show Gist options
  • Save EmmanuelGuther/060e1521ad100b05236898999c46be9f to your computer and use it in GitHub Desktop.
Save EmmanuelGuther/060e1521ad100b05236898999c46be9f to your computer and use it in GitHub Desktop.
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