15

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
}
Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35

2 Answers2

19

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!

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
RyuHayaboosa
  • 315
  • 2
  • 2
  • 14
    It doesn't give a call back when there will be no text change that is when s.length() = 0, what ever key is pressed there will be no call back.. – DJphy Jun 10 '15 at 06:01
  • 6
    There is no callback received in `onTextChanged` when back button is pressed and there is no character in the `EditText`. Not sure why this answer even exits here! – sud007 May 18 '17 at 10:05
  • if typed one is white space then also count == 0 – Bincy Baby Aug 21 '17 at 09:21
15

A KeyListener can fulfil both of your conditions.

mEditText.setOnKeyListener(new OnKeyListener() {                 
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
           if(keyCode == KeyEvent.KEYCODE_DEL){  
             //on backspace
             }
    return false        
        }
});

Similarly inside the onKey(), you can put multiple check statements to check for the condition, when you would want to clear the textView.

EDIT : As @RankoR was kind enough to point out, please bear in mind that onKeyListener() works only for the hardware keyboards and not the soft keyboards.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • 5
    OnKeyListener works only for HW keyboards. – artem Mar 28 '13 at 04:31
  • @RankoR : Yes, I am aware of the fact the onKeyListener does not work for the soft-keyboard. But if you went through the question once again, you would see that the question is not specific for the soft-keyboard. But, yes..then again, you have made a pretty much valid point. Will edit my answer to include your point. – Swayam Mar 28 '13 at 16:33
  • @RankoR :Edited the answer. Maybe you can reconsider your *downvote* now. :D – Swayam Mar 28 '13 at 16:39
  • 1
    Thanks, removed -1 and added +1 ;) – artem Mar 28 '13 at 17:14
  • Now, that is really considerate of you! Thanks a ton! :D – Swayam Mar 28 '13 at 17:58
  • This doesnt work on Android 4.1 – Cuong Thai Jun 03 '13 at 11:01
  • Thank you for your input. I will look into the matter and see if I can get something to work. Or do you already have a solution for 4.1..? – Swayam Jun 03 '13 at 19:23