26

I was reading about TextWatcher in Android programming. I could not understand the difference between afterTextChanged() and onTextChanged().

Although I referred to Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged, I am still not able to think of a situation when I would need to use onTextChanged() and not afterTextChanged().

0xCursor
  • 2,242
  • 4
  • 15
  • 33
SimpleGuy
  • 2,764
  • 5
  • 28
  • 45

3 Answers3

25

I found an explanation to this on Android Dev Portal

http://developer.android.com/reference/android/text/TextWatcher.html

**abstract void afterTextChanged(Editable s)**
This method is called to notify you that, somewhere within s, the text has been changed.

**abstract void beforeTextChanged(CharSequence s, int start, int count, int after)**
This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after.

**abstract void onTextChanged(CharSequence s, int start, int before, int count)**
This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.

So, the differences between the two are:

  • I can change my text using afterTextChanged while onTextChanged does not allow me to do that
  • onTextChanged gives me the offset of what changed where, while afterTextChanged does not
SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
6

Just adding something to Pratik Dasa's answer and the discussion with @SimpleGuy in the comments, since I have not enough reputation to comment.

The three methods are also triggered by EditText.setText("your string here"). That would make a length of 16 (in this case), so count isn't always 1.

Please note that the parameter list is not the same for the three methods:

abstract void afterTextChanged(Editable s)
abstract void beforeTextChanged(CharSequence s, int start, int count, int after)
abstract void onTextChanged(CharSequence s, int start, int before, int count)

And this is where the difference is between afterTextChanged and onTextChanged: the parameters.

Please also have a look at the accepted answer in this thread: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

Community
  • 1
  • 1
Annenarg
  • 141
  • 2
  • 7
-6

here is the explanation:

onTextChanged : This means when you start typing, like you want to write "sports" then this will call on each and every character, like it will call when you pressed "s" then again "p" then "o" and so on...

afterTextChanged : This will call when you stop typing, it will calls after you completely wrote "sport", that is the main diffrence.

YOUR_EDIT_TEXT.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    //Your query to fetch Data
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (s.length() > 0) {

                            //Your query to fetch Data
                        }
                    }
                });
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • 1
    But, how does it know I stopped typing ? I give pause (even nanosecs) but it is a pause.. and android does not know that I am going to type "s" "p" .. so on.. how does it knows that I am going to type further.. like I type "spor" .. done.. so will it be called ? or say I type "sport" then will it be called.. if yes.. then it is called twice.. sory can't understand the diff – SimpleGuy Nov 18 '14 at 11:37
  • 1
    @SimpleGuy Ofcourse Android knows it, OS have some criteria obviously to fetch the delay between typed character and using it could conclude. Android must have some criteria to identify it. – Pratik Dasa Nov 18 '14 at 11:39
  • I read at android developer portal. [link](http://developer.android.com/reference/android/text/TextWatcher.html#onTextChanged(java.lang.CharSequence, int, int, int)) It says for **onTextChanged** - "This method is called to notify you that, within s, the count characters beginning at start have just replaced" - why "characters" (plural) when you say it is for each character ? Why would count be needed at all, if you say that it is called for each character (count = 1) – SimpleGuy Nov 18 '14 at 12:27
  • @SimpleGuy Docs is right, in onTextChanged() method, when you type characters it will replaces each character with previous one typed. Right!!! – Pratik Dasa Nov 18 '14 at 12:28
  • So basically.. I am still not clear about the difference – SimpleGuy Nov 18 '14 at 12:28
  • Don't get me wrong.. i appreciate your efforts.. but it is that I am not able to understand the difference – SimpleGuy Nov 18 '14 at 12:30
  • @SimpleGuy its crystal clear now, I dont know why you can not getting this. I suggest you to if you can not getting things then please adopt the thing which is mentioned in docs, becase dont waste your time to find out the answer, you will get it soon once when you will be champ in development. – Pratik Dasa Nov 18 '14 at 12:30
  • 6
    Based on some tests now, this answer ("will fire when stopped typing") is not correct. Both methods fire at the same time, and with the same s.toString() content – PVS Jun 07 '16 at 21:53
  • @SimpleGuy One example of why it is "characters" (plural) is because of pasting from the clipboard. The user can select a word of text and paste a previously copied word over it. Then count and after will both be > 1. – leorleor May 24 '17 at 23:02
  • complete nonsense, this answer should be blocked. If it worked so, on-the-fly text formatting would be impossible. – Dmitry Gryazin Aug 03 '17 at 16:56