Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How can I do It?

like image 314
PhillyTheThrilly Avatar asked Oct 11 '19 22:10

PhillyTheThrilly


People also ask

How do I know if EditText has changed?

Implement a TextWatcher. It gives you three methods, beforeTextChanged , onTextChanged , and afterTextChanged . The last method shouldn't be called until something changes anyway, so that's a good thing to use for it.

How do I delete a field in EditText?

just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box. this should be the accepted answer, +1 for simplicity.

How do you use backspace on Android?

When you're swiping and a word appears that isn't what you actually wanted, tap Gboard's Backspace key once. That'll erase the entire word in one fell swoop, and then you can swipe it out again (or maybe just manually peck it in) to get it right.


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;
    }
 });
like image 122
Alireza Bideli Avatar answered Sep 28 '22 20:09

Alireza Bideli


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

like image 40
ikerfah Avatar answered Sep 28 '22 19:09

ikerfah