Skip to content

Instantly share code, notes, and snippets.

@thasneemp
Last active May 13, 2025 08:13
Show Gist options
  • Select an option

  • Save thasneemp/f94ac09d8077770df6f90ad7bb54ead5 to your computer and use it in GitHub Desktop.

Select an option

Save thasneemp/f94ac09d8077770df6f90ad7bb54ead5 to your computer and use it in GitHub Desktop.
Android EditText text change with flow coroutine debounce
fun EditText.textChanges(): Flow<String> {
return callbackFlow {
val listener = doOnTextChanged { text, _, _, _ -> trySend(text.toString()) }
awaitClose { removeTextChangedListener(listener) }
}.onStart { emit(text.toString()) }
}
private fun searchQuery(searchQuery: String): Flow<List<String>> {
val items = listOf("Hello", "How are You", "Oh no!", "Iam Good!")
return flow {
delay(200)
emit(items.filter { it.contains(searchQuery) })
}
}
getDataBinding().userNameEditText.textChanges().filterNot { it.isBlank() }
.debounce(300).distinctUntilChanged().flatMapLatest { searchQuery(it) }
.onEach {
Log.d("SEARCH_TEST", it.toString())
}.launchIn(lifecycleScope)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment