I am filtering my list using an EditText. I want to filter the list 0.5 second after user has finished typing in EditText. I used the afterTextChanged
event of TextWatcher
for this purpose. But this event rises for each character changes in EditText.
What should I do?
You can use
EditorActionListener
for that purpose.You can use RxBindings, it's the best solution. See guide to RxJava operator debounce, I'm sure that will do great in your case.
http://reactivex.io/documentation/operators/debounce.html
The trick is in canceling and re-scheduling
Timer
each time, when text inEditText
gets changed. Good luck!UPDATE For those interested in how long to set the delay, see this post.
Non of the above solution worked for me.
I needed a way for TextWatcher to not fire on every character I input inside my search view and show some progress, meaning I need to access UI thread.
Removing Handler on every onTextChanged (which is called when the user inputs a new character). afterTextChanged is called after the text has been changed inside input field where we can start new Runnable, but will cancel it if user types more characters (For more info, when these callback are called, see this). If user doesn't input anymore characters, interval will pass in postDelayed and it will call work you should do with that text.
This code will run only once per interval, not for every key user inputs. Hope it helps someone in the future.