Skip to content

Instantly share code, notes, and snippets.

@Sardorbekcyber
Created October 7, 2021 13:16
Show Gist options
  • Save Sardorbekcyber/573e7d6f7ac3e007f1cb60e43936bade to your computer and use it in GitHub Desktop.
Save Sardorbekcyber/573e7d6f7ac3e007f1cb60e43936bade to your computer and use it in GitHub Desktop.
This example demonstrates how to override TextWatcher's only after text changed method
fun TextInputEditText.textChangedDebounce(debounceTime: Long = 100L, action: (text: String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
private var lastTextChangedTime: Long = 0
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
if (SystemClock.elapsedRealtime() - lastTextChangedTime < debounceTime) return
else action(s.toString())
lastTextChangedTime = SystemClock.elapsedRealtime()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment