Reputation: 23
I have a EditText
box and the user types a string and the string is sent to a database and the UI
is updated. I used the TextChanged
function and the function works properly. However rather than updating the UI
after each keystroke I want to wait 1/2 a second after the last keystroke to update the string to send to the database. Any suggestions on implementation?
editText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
{
string text = editText.Text.ToString();
};
Upvotes: 2
Views: 2590
Reputation: 2106
We can actually use text-watcher override functions to solve this problem
Below is an example as to how it can be done
Also attached link with complete description
private EditText searchText;
private TextView resultText;
private Timer timer;
private TextWatcher searchTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// user typed: start the timer
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// do required
}
}, 400); // 400ms delay to before executing run, if user stops typing after 400ms function will get fired
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// reset timer if user is typing
if (timer != null) {
timer.cancel();
}
}
};
source : delayed edit text
Upvotes: 0
Reputation: 1346
You can do following before updating the ui
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
//Upadating your ui
}
}, 500);
Upvotes: 0
Reputation: 11211
This can do it:
new Handler().postDelayed(new Runnable(
public void run(){
// your update code here..
}
), 2000);
2000 represents the number of milliseconds before the code will run.. so in this case is 2 seconds.
Though I think you might try to think to a better idea of saving the data, like using onFocusChangeListener
on the EditText
and save the data after the EditText
looses focus..
EDIT:
Of course the above code should be put in the text changed callback method and you should also make sure the timer won't be called on each key pressed..
Upvotes: 3