Change the text of the EditText to onTextChangeListener ()

I am working on an Android application. In my application, I have to use text based images. Therefore, I am writing OnChangeListener() for EditText . The following is my sample code.

 edt.addTextChangedListener(this); @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub CharSequence cs=convert(edt.getText.toString()); edt.setText(cs); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } 

But I get an exception for the above code. I know that the cause of the exception is the call to the setText() method from afterTextChanged() . But I need to change the text value of the EditText based on the same change in the text of the EditText . Help me friends

+4
source share
2 answers

Just delete your listener before you set the text, and register it again when you finish, as described here:

Clear EditText text after adding onTextChanged implementation

+1
source

Another solution would be to use a boolean variable, so it does not fall into an infinite column and ultimately gives a stackoverflow exception

 public void afterTextChanged(Editable s) { if(!flag) { flag = true; edt.setText("string"); flag = false; } } 
+7
source

All Articles