javaandroidinner-classesandroid-textwatcher

when setText on editText TextWatcher.onTextChanged not called


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 ?


Solution

  • 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:");
    }