How to update the same EditText using TextWatcher?
Asked Answered
Q

4

5

In my Android application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With some extra string. When I try to do this the program terminates.

 final EditText ET = (EditText) findViewById(R.id.editText1);
 ET.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            try
            {
                 ET.setText("***"+ s.toString());
                 ET.setSelection(s.length());
            }
            catch(Exception e)
            {
                Log.v("State", e.getMessage());
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s)
        {               
        }
    });

My program terminates and even I try to catch the exception like in my code still it terminates. Does anyone have any idea why this happens and how I can achieve this? Thanks.

Quaky answered 29/2, 2012 at 11:21 Comment(1)
When I try to do this the program terminates. that basically means that you should provide the stack trace that can be found in the LogCat. That would help us a lot!Schaffhausen
C
12

The content of the TextView is uneditable on the onTextChanged event.

Instead, you need to handle the afterTextChanged event to be able to make changes to the text.

For more thorough explanation see: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged


Note: Error onTextChanged

Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.

From the ref:

public abstract void afterTextChanged (Editable s)
This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. ...

  • Suggestion 1: if you can, check if the s is already what you want when the event is triggered.

    @Override
    public void afterTextChanged(Editable s)
    {    
        if( !s.equalsIngoreCase("smth defined previously"))
             s = "smth defined previously";              
    }
    
  • Suggestion 2: if you need to do more complex stuff (formatting, validation) you can maybe use a synchronized method like in this post.

Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( ****four)

You can use something like this in suggestion 1:

    @Override
    public void afterTextChanged(Editable s)
    {    
       String sText = ET.getText().toString()

        if( !isFormatted(sText))
             s = format(sText);              
    }
    bool isFormatted(String s)
    {
     //check if s is already formatted
    }

    string format(String s)
    {
      //format s & return
    }
Chericheria answered 29/2, 2012 at 11:25 Comment(5)
HI Zorkun, I tried adding same code in the afterTextChanged(Editable s) event... Still it terminates... Do you know any other alternative way of doing this? ThanksQuaky
The s is editable within afterTextChanged(Editable s). But still it can not assigned to the EditText.Quaky
Yes That's true. It causing an endless loop by continuously changing the text on afterTextChanged event. The thing is, I want to hide the first letters in the EditText, With Some character as * and let only last 4 characters to be visible...Quaky
Thanks... Yes can restrict it running over and over with a simple if condition... Thank you very much for your time and help...!!!!Quaky
Cannot convert from String to Editable.Walt
B
2

To supplement Zortkun's answer (where the example code is quite broken), this is how you'd use afterTextChanged() to update the same EditText:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {    
    }

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

    @Override
    public void afterTextChanged(Editable editable) {        
        if (!editable.toString().startsWith("***")) {
            editable.insert(0, "***");
        }        
    }
});

Get familiar with the Editable interface to learn about other operations besides insert().

Note that it's easy to end up in an infinite loop (the changes you do trigger afterTextChanged() again), so typically you'd do your changes inside an if condition, as above.

As afterTextChanged() javadocs say:

It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively.

Bartizan answered 2/8, 2016 at 12:40 Comment(0)
B
2

late answer, if someone looking this is how i did it.

  • set addTextChangedListener initially
  • in one of the call back (say onTextChanged()) remove addTextChangedListener
  • Still interested in receiving updates add it back again.

here is the code.

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d("log", "before");
        }

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

            Log.d("log", "after");
            editText.removeTextChangedListener(this);

            ediText.setText("text you wanted to put");

            editText.addTextChangedListener(this);

        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });
Bohn answered 12/4, 2017 at 17:17 Comment(0)
G
0

Here is a snippet that worked for me

etPhoneNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().equals(Utils.getFormattedNumber(s.toString()))) {
                s.replace(0, s.length(), Utils.getFormattedNumber(s.toString()));
            }
        }
    });

where Utils.getFormattedPhoneNumber() is your method returning a formatted number

Gonfanon answered 27/2, 2018 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.