Skip to content

Instantly share code, notes, and snippets.

@rommansabbir
Last active February 13, 2021 18:19
Show Gist options
  • Save rommansabbir/430e24a37669ae1738c7ad1ace4ab74f to your computer and use it in GitHub Desktop.
Save rommansabbir/430e24a37669ae1738c7ad1ace4ab74f to your computer and use it in GitHub Desktop.
TextView after text changed with delay Extension function
inline fun TextView.afterTextChangedDelayed(crossinline afterTextChanged: (String) -> Unit): TextWatcher {
val watcher = object : TextWatcher {
private var timer: Timer = Timer()
// Change the delay based on your requirement
private val DELAY: Long = 1000
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 = Timer()
timer.schedule(
object : TimerTask() {
override fun run() {
Handler(Looper.getMainLooper()).post {
afterTextChanged.invoke(editableText.toString())
}
}
},
DELAY
)
}
}
this.addTextChangedListener(watcher)
return watcher
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment