I am using TextWatcher
and I am unable to detect Backspace key in TextWatcher.afterTextChange
event. I also want to clear textView
on some condition in textWatcher
event.
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// I want to detect backspace key here
}
To detect a backspace in TextWatcher
, you can check the variable count that is passed into the onTextChange
function (count will be 0 if a backspace was entered), like this:
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
if (react) {
if (count == 0) {
//a backspace was entered
}
//clear edittext
if(/*condition*/) {
react = false;
setText("");
react = true;
}
}
}
The react boolean
is needed for the setText
() function otherwise it becomes recursive. Hope this helps!