16

I need to replace the text inside the EditText while typing : Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D"). Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :

final EditText et = (EditText) findViewById(R.id.editTexts);
    final TextView tv = (TextView) findViewById(R.id.textView2);

    et.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) {
                if(s.length() > 0) {
                    tv.setText(s.toString().substring(s.length()-1));
                    et.setText("");
                }
            }
    });
Issam Zoli
  • 2,724
  • 1
  • 21
  • 35

2 Answers2

29

You can change it as required::

public class SampleActivity extends Activity {
    TextWatcher tt = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et = (EditText) findViewById(R.id.editText1);
        final TextView tv = (TextView) findViewById(R.id.textView1);
        tt = new TextWatcher() {
           public void afterTextChanged(Editable s){
                et.setSelection(s.length());
           }
           public void beforeTextChanged(CharSequence s,int start,int count, int after){} 
           public void onTextChanged(CharSequence s, int start, int before, int count) {
               et.removeTextChangedListener(tt);
               et.setText(et.getText().toString().replace("A", "C"));
               et.addTextChangedListener(tt);
           }
       };
       et.addTextChangedListener(tt);
   }
}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • et replaces "a" with "c" but it's reversed it adds characters at the beginning of the field – Issam Zoli Jun 02 '12 at 14:00
  • 1
    Maybe it needs a ET.setSelection(s.length()); somewhere !! – Issam Zoli Jun 02 '12 at 14:03
  • 1
    I'm getting a compiler error in the calls `et.removeTextChangedListener(tt)`. It complains that `tt` might not be initialized. You can avoid this by simply substituting `this` for `tt`. – SMBiggs Jan 16 '19 at 17:13
  • aren't you calling `addTextChangedListener` twice? one call inside `onTextChanged` and other in `onCreate` – voghDev Nov 09 '20 at 11:33
9

In order to change the text interactively, you need to register a TextWatcher. But trying to change the text inside the watcher creates further calls to the watcher. My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after

    mEditText.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) {
            mEditText.removeTextChangedListener(this);
            mEditText.setText(//TODO change whatever you like here);
            mEditText.setSelection(editable.length()); //moves the pointer to end
            mEditText.addTextChangedListener(this);
        }
abedfar
  • 1,989
  • 24
  • 21