I want to get a callback when a character is deleted in an EditText.
How can I do It?
Implement a TextWatcher. It gives you three methods, beforeTextChanged , onTextChanged , and afterTextChanged . The last method shouldn't be called until something changes anyway, so that's a good thing to use for it.
just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box. this should be the accepted answer, +1 for simplicity.
When you're swiping and a word appears that isn't what you actually wanted, tap Gboard's Backspace key once. That'll erase the entire word in one fell swoop, and then you can swipe it out again (or maybe just manually peck it in) to get it right.
There is no CallBack for Removing charactor directly !!
But each time you add any text or edit your EditText text all of TextWatcher CallBacks called Respectively
(1-beforeTextChanged , 2-onTextChanged, 3-afterTextChanged)
Therefore you can check delete operation in all of them as below. Notice that you don't need to check delete operation in all callbacks . There are 3 ways to understand delete operation in TextWatcher in 3 TextWatcher CallBacks and each of them can solve your problem :)
.I think it is better for you to know about some of TextWatcher callBacks arguments.
As @ikerfah said
Ways :
onTextChanged
called . compare your
field which is previous EditText count with count argument which is
current EditTextCount;onTextChanged
listener but just you use length instead of count.Change Your Final addTextChangedListener link below:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
if (after < count) {
// delete character action have done
// do what ever you want
Log.d("MainActivityTag", "Character deleted");
}
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//mPreviousCount count is fied
if (mPreviousCount > count) {
// delete character action have done
// do what ever you want
Log.d("MainActivityTag", "Character deleted");
}
mPreviousCount=count;
}
@Override
public void afterTextChanged(Editable editable) {
Log.d("MainActivityTag",editable.toString());
int length=editable.length();
//mPreviousLength is a field
if (mPreviousLength>length)
{
// delete character action have done
// do what ever you want
Log.d("MainActivityTag", "Character deleted");
}
mPreviousLength=length;
}
});
editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
new inside your beforeTextChanged callback you have three parameters
now you need only to compare between those parameters to achieve anything you desire with the callback beforeTextChanged
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With