How to know a delete operation occurred in EditText in Android?

Viewed 1625

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

How can I do It?

2 Answers

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;
    }
 });
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

Related