I currently have some action associated with when the user types something in a AutocompleteTextView. However, I only want the action to register if the user stops typing. Essentially, I don’t want the action to happen while the user to spam typing. The following code might explain it a little better.
AutocompleteTextView search = ...;
search.setOnKeyListener(new onKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (mTimer != null)
mTimer.cancel();
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
public void run() {
my_func();
}
}, 1000);
return true;
}
return false;
}
}
public void my_func() {
Looper.prepare();
// Do some function that changes the View
Looper.loop();
}
Any ideas how I can force the keyListener to wait a certain number of seconds?
EDIT:
I am now using Timer to schedule the function. Refer to above edited code.
The FIRST timer works. However, when I try to re-evoke the function the second timer, I get an exception that says only main thread that initialized the View can modify the View. I am not sure how Looper works (I just added Looper because it complained before that I need to use it). Any suggestions?
Create a Timer. When the Timer fires, do your action. If you receive another keypress before the Timer expires, cancel the timer, and reset it. Something like,
If you modify the view tree from inside the timer task’s
run()method, you must force it to happen on the UI thread. You can do this like,