I have an EditText. When i click on it, it becomes focusable. I will type the input text to be entered into the EditText. I want to implement a listener for EditText, so that when i stop typing, it should automatically save that text into the database instead of having a button. How to have a listener for EditText to listen that typing is stopped or not?
Asked
Active
Viewed 3.7k times
4 Answers
57
Try like this.
EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {
Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
}
});
![](../../users/profiles/819151.webp)
jainal
- 2,903
- 1
- 17
- 18
17
set edittext imeOption
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
By using something like this,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Specify your database function here.
return true;
}
return false;
}
});
Alternatively, you can use the OnEditorActionListener
interface to avoid the anonymous inner class.
![](../../users/profiles/356895.webp)
JJD
- 44,755
- 49
- 183
- 309
![](../../users/profiles/959712.webp)
vajapravin
- 1,343
- 3
- 16
- 28
-
You can also close the virtual keyboard after Done is clicked by returning false instead of returning true. – Youness Feb 05 '17 at 23:16
7
Add This to your editText
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
![](../../users/profiles/5441148.webp)
Ashwani Srivastava
- 81
- 1
- 4
0
Also you can use debounce operator of Rx java.
subject.debounce(300, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(yourString -> {
// save to db
}
return "";
})
.subscribe()
Here you can define time limit , after how much time you want it to get saved in db.
![](../../users/profiles/8389762.webp)
pandey_shubahm
- 185
- 2
- 11