Textwacher loops after calling append

textwatcher in android kotlin
android textwatcher for multiple edittext example
textwatcher in recyclerview android
android textwatcher validation
android textwatcher change text
aftertextchanged called multiple times
android-edittext change text while typing
addtextchangedlistener not working

I have a problem with TextWatcher. After i am done editing, i want to append " kg" string. But after i try to edit editView, it loops and my app crashes. Can somebody help me? Thanks

weight = findViewById(R.id.editText);
        weight.addTextChangedListener(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) {
                editable.append(" kg");
            }
        });

You can manage this with an if statement but I think that it can down in better way

    weight.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable editable) {

            if(!editable.getText().contains("kg"))
               editable.append(" kg");
        }
    });

Textwacher loops after calling append, I have a problem with TextWatcher. After i am done editing, i want to append " kg" string. But after i try to edit editView, it loops and my app crashes. Implementing Text watcher is quite easy, you have to call addTextChangedListener() method and pass the reference to TextWatcher instance. You can override the following TextWatcher class method to take various actions when the content of EditText changes. afterTextChanged (Editable s) - This method is called when the text has been changed

Its looping because setting the text calls afterTextChanged. What you could do is remove the text watcher, change the text, then re-add the text watcher.

Android Notes 66: How to use TextWatcher for more than one , However, TextWatcher is a pain ass when we want to use more than one listener and after we're done with that EditText, just add the listener again. after we use setText(), it recalls the listener and it creates an infinite loop. 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. It is an error to attempt to make changes to s from this callback.

Follow these steps:

  1. Remove the TextWatcher temporarily
  2. Do your changes to your content
  3. Add the TextWatcher again

    weight.addTextChangedListener(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.toString().contains("kg")) return
             weight.removeTextChangedListener(this);
             editable.append(" kg");
             weight.addTextChangedListener(this);
         }
     });
    

TextWatcher, When an object of this type is attached to an Editable, its methods will to get yourself into an infinite loop, because any changes you make will  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:

infinite loop in EditText TextWatcher - android, Android: infinite loop in EditText TextWatcher - android. updating your edittext as it will keep calling your text change listener.. litera. not erase after the first position because when EditText length is 1, it will add a ' at the second position. 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().

TextWatcher events are being fired multiple times, For some reason the calls to the TextWatcher events upon one text change a loop a few times to get it right, and you might only get your first call after it has #​Override public void afterTextChanged(Editable s) { } }; Add the TextWatcher in  TextWatcher Basically, a TextWatcher is used to keep watch on EditText while entering data into it. We can perform operation and keep watch on which characters are being entered or how many characters are being entered in the EditText. For Ex.

How to use Text Watcher to get values from a List View with multiple , TextWatcher is used to keep track/watch on a EditText and to trigger when It allows us to track each character when value of the… Just call the method addTextChangedListener() and pass the You have to be careful when using this method otherwise it could lead your program into a infinite loop !!! Android EditText with TextWatcher (Searching data from ListView) Android EditText is a subclass of TextView.EditText is used for entering and modifying text. While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input.

Comments
  • Look here: stackoverflow.com/a/14195571/9495357
  • With few changes your idea works good. I couldnt use editable, because it doesnt have getText method. I just changed editable with weight and it works. thank you
  • Tried your idea, it no longer loops but every time i press any key it adds " kg".
  • The messy solution i can think of is to set the editable to editable.text.replaceFirst(" kg", "") and then append " kg" to the end of that
  • Tried this already. Every time i press a key ( letter, number, backspace or any other) it adds " kg".
  • this edit would likely cause it to add kg the first time you enter a character and then never add kg again