when setText on editText TextWatcher.onTextChanged not called
Asked Answered
M

4

5

Whenever EditText string is changed, onTextChanged is called.

Now when I start a new Activity and send data through Bundle, onTextChanged is not called.

if( getIntent().getExtras() != null) {
    Bundle b = getIntent().getExtras();
    int value = -1;

    if(b != null)
        value = b.getInt("key");
    edit1.setText("Mywords:");
}

How can I call it ?

Mallee answered 28/8, 2016 at 9:56 Comment(0)
J
8

So here's the modified version of your code. The idea is to set the text in the EditText after you add a TextWatcher on it.

if( getIntent().getExtras() != null) {
    Bundle b = getIntent().getExtras();
    int value = -1;

    if(b != null)
        value = b.getInt("key");

    // Add  the TextWatcher here
    edit1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Toast.makeText(MainActivity.this, "before text changed", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Toast.makeText(MainActivity.this, "on text changed", Toast.LENGTH_LONG).show();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Toast.makeText(MainActivity.this, "after text changed", Toast.LENGTH_LONG).show();
        }
    });

    // Now the set the value in your EditText
    edit1.setText("Mywords:");
}
Jetpropelled answered 28/8, 2016 at 10:28 Comment(2)
Working Fine. ThanksIglesias
Good to know that helped!Jetpropelled
V
3

Make sure you are subscribing with TextWatcher before setText called.

Vines answered 28/8, 2016 at 10:19 Comment(2)
you mean implementing TextWatcher class?Mallee
I mean that call to edit1.addTextChangedListener should be prior to edit1.setText.Vines
B
1

For some reason, the method addTextChangedListener is not called for me.

However the method doBeforeTextChanged, doOnTextChanged and doAfterTextChanged worked perfectly :

amountTextField.doBeforeTextChanged { text, start, count, after ->
    Log.d(TAG, "beforeTextChange.") // OK, called
}
amountTextField.doOnTextChanged { text, start, before, count ->
    Log.d(TAG,  "onTextChanged.") // OK, called
}
amountTextField.doAfterTextChanged {
    Log.d(TAG, "afterTextChanged.") // OK, called
}
amountTextField.addTextChangedListener {
    object:  TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            Log.d(TAG, "beforeTextChange.") // NOK, not called
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            Log.d(TAG,  "onTextChanged.") // NOK, not called
        }

        override fun afterTextChanged(p0: Editable?) {
            Log.d(TAG,  "onTextChanged.") // NOK, not called
        }

    } }

By the way, the result was always the same : whether I set any text to my EidtText or not, whether after or before I set any text.

Balderas answered 7/2, 2022 at 15:14 Comment(0)
A
0

Try the following:

edit1.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {}

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

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

Reference: android on Text Change Listener

Albite answered 28/8, 2016 at 10:0 Comment(2)
i already used it in the same way, u mean every time it change text using setText i have to create inner class every time?Mallee
no, you don't have to create the inner class every time, please, give us your code to can help you.Albite

© 2022 - 2024 — McMap. All rights reserved.