Android Notes 66: How to use TextWatcher for more than one EditText? [v2 UPDATED]

Kuray Ogun
FreakyCoder Software Blog

--

Updated: March 2, 2020

TextWatcher itself is awesome. It provides us with a lot of easiness. However, TextWatcher is a pain ass when we want to use more than one EditText at the same time.

How to?

If you’re unlucky like me, you need to waste hours for solving this issue. Luckily, the solution is so simple. We just need to remove the current listener and after we’re done with that EditText, just add the listener again.

First, we simply create a TextWatcher. There are two important points here.

hashCode(): We use if check hashCode for each EditText because hashCode gives us the which EditText we are using right now.

Add & Remove TextChangedListener: We have to remove and re-add the textChangedListener because after we use setText(), it recalls the listener and it creates an infinite loop. So we simply remove the listener BEFORE what we do with the editText and re-add it when we’re done.

Java Version

Code:

TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
if (editable != null && !editable.toString().equalsIgnoreCase("")){
// Checking editable.hashCode() to understand which edittext is using right now
if (editText1.getText().hashCode() == editable.hashCode()){
// This is just an example, your magic will be here!
String value = editable.toString();
editText1.removeTextChangedListener(textWatcher);
editText1.setText(value);
editText1.addTextChangedListener(textWatcher);
}
} else if (editText2.getText().hashCode() == editable.hashCode()){
// This is just an example, your magic will be here!
String value = editable.toString();
editText2.removeTextChangedListener(textWatcher);
editText2.setText(value);
editText2.addTextChangedListener(textWatcher);
}
}

Gist:

DO NOT FORGET TO ADD TEXTWATCHER LISTENER AT THE BEGINNING!

editText1.addTextChangedListener(textWatcher);
editText2.addTextChangedListener(textWatcher);

Kotlin Version

Code:

val textWatcher = object : TextWatcher {
override
fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2:Int) {

}

override
fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

}

override
fun afterTextChanged(editable: Editable?) {
if (editable != null && !editable.toString().equals("")) {
// Checking editable.hashCode() to understand which edittext is using right now
if (editText.editText!!.text.hashCode() === editable.hashCode()) {
// This is just an example, your magic will be here!
val value = editable.toString()
editText.editText!!.removeTextChangedListener(this)
editText.editText!!.setText(value)
editText.editText!!.addTextChangedListener(this)
}
} else if (editText2.editText!!.text.hashCode() === editable!!.hashCode()) {
// This is just an example, your magic will be here!
val value = editable!!.toString()
editText2.editText!!.removeTextChangedListener(this)
editText2.editText!!.setText(value)
editText2.editText!!.addTextChangedListener(this)
}
}
}

Kotlin Gist:

Text Watcher Kotlin Gist

That’s it! If you have any question, ask me :)

--

--