Hey,
TextWatcher itself is awesome. It provides us 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, 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 in 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 a infinite loop. So we simply remove the listener BEFORE what we do with the editText and re-add it when we’re done.
Markdown:
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:
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);
}
}
}
DO NOT FORGET TO ADD TEXTWATCHER LISTENER AT THE BEGINNING !
editText1.addTextChangedListener(textWatcher);
editText2.addTextChangedListener(textWatcher);
Kotlin Markdown:
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:
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) } } }