Last active
February 13, 2021 18:19
-
-
Save rommansabbir/430e24a37669ae1738c7ad1ace4ab74f to your computer and use it in GitHub Desktop.
TextView after text changed with delay Extension function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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