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 ?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
phpdroid
  • 1,642
  • 1
  • 18
  • 36

4 Answers4

7

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:");
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
3

Make sure you are subscribing with TextWatcher before setText called.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
j2ko
  • 2,479
  • 1
  • 16
  • 29
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.

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

Community
  • 1
  • 1
Mahmoud Ibrahim
  • 1,057
  • 11
  • 20