7

I want to get a callback when a character is deleted in an EditText.

How can I do It?

PhillyTheThrilly
  • 1,562
  • 2
  • 16
  • 21

2 Answers2

5
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

  • start is the start index that is about to be deleted
  • count is the length of the text that is about to be deleted
  • after is the length of the text that is about to be added

now you need only to compare between those parameters to achieve anything you desire with the callback beforeTextChanged

ikerfah
  • 2,612
  • 1
  • 11
  • 18
5

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

  • start is the start index that is about to be deleted
  • count is the length of the text that is about to be deleted
  • after is the length of the text that is about to be added

Ways :

  1. beforeTextChanged: you compare after argument with count argument .
  2. onTextChanged: you declare a field in your activity or fragment and fill the field each time onTextChanged called . compare your field which is previous EditText count with count argument which is current EditTextCount;
  3. afterTextChanged: It is pretty like 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;
    }
 });
Alireza Bideli
  • 834
  • 2
  • 9
  • 31
  • This solution is not a good solution. You can end up with less number of characters even without a delete operation. For example, when you type, the keyboard gives you suggestions, and those suggestions might be smaller in length than the current text. So your solution will wrongly mark suggestions as delete case. – Amol Jindal Sep 13 '21 at 21:41