I have an EditText field in my layout. I want to perform an action when the user stops typing in that edittext field. I have implemented TextWatcher and use its functions
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { }
@Override
public void afterTextChanged(Editable editable) {
}
The function onTextChanged
and afterTextChanged
get called just after typing any character, but I want to perform action after the user has finished typing in that edittext field, just like facebook does on it's "Check In" page.
How can I implement this?
If you use Kotlin, use this extension function:
Use example:
I used a CountDownTimer to find out if the user stopped typing after a while. I hope to help.
This is how I did and works for me!
It is a little different kind of approach. But you can do something like this
I am assuming that after a user started typing in the edittext, and he didn't typed for a particular time period than it can be considered that he stopped typing.
Basically what you are doing here is, that whenever a user starts typing, if there is a time gap of 5000 milliseconds in changing the text inside the edittext, you consider as the user has stopped typing. Of course you can change the time to whatever you want
You could use a focus change listener. If the edittext has focus then assume user still editing otherwise they have stopped and you can perform your action:
There is a very simple and easy way of doing this using
RxBindings
for Android,Get the library if you haven't already,
Add and modify this snippet to your needs,
What is Debounce Operator? - Learn more about it here
In our context, debounce will only notify you of the text change inside the
EditText
after a certain time has passed from the last text change that occurred inside theEditText
.Here I am waiting for 3 seconds until the last text change happened to conclude that the user has stopped typing. You can modify it according to your needs.