Im making a program where a user has to search through some of my data.
I've created an edit view for the user to put their text in to, but I want to run a method once the user has confirmed their input.
How do I do this?
Thank you.
Im making a program where a user has to search through some of my data.
I've created an edit view for the user to put their text in to, but I want to run a method once the user has confirmed their input.
How do I do this?
Thank you.
If you are trying to add a search feature , why not use a search view instead.
there are many options to do this, i am writing 3 options below.
1 - If you have confirm button then use its onclicklistener for you method call.
2 - If you don't have confirm button then add focusChangeListener in edittext and call you method when edittext focus is lost.
3 - You can use a timerTask like when user stopped typing for 2 second (we assume that user input is done) you can call your method
Example:
edt_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// user is typing: reset already started timer (if existing)
if (timer != null) {
timer.cancel();
}
}
@Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Search Logic
if (!s.toString().trim().isEmpty())
// Your Function call
}
}, 500); // 350ms delay before the timer executes the "run“ method from TimerTask
}
}