Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTextChanged vs afterTextChanged in Android - Live examples needed

Tags:

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().

like image 464
SimpleGuy Avatar asked Nov 18 '14 10:11

SimpleGuy


People also ask

What is the difference between onTextChanged and afterTextChanged Android?

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.

What is afterTextChanged in Android?

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)

How do I use addTextChangedListener?

While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input. EditText uses TextWatcher interface to watch change made over EditText. For doing this, EditText calls the addTextChangedListener() method.


Video Answer


1 Answers

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
like image 70
SimpleGuy Avatar answered Sep 21 '22 11:09

SimpleGuy