I want to get a callback when a character is deleted in an EditText.
How can I do It?
I want to get a callback when a character is deleted in an EditText.
How can I do It?
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