title: Changing text in Android on text change causes overflow error
body: <p>I want to create an application that can transform text the user enters in an <code>EditText</code> widget in real time, and I've added a <code>TextWatcher</code> to allow me to do stuff on text change, but it's causing an overflow error because I'm basically creating an endless loop (<code>onTextChange -&gt; code to change text -&gt; onTextChange -&gt; etc...</code>).</p>
  
  <p>Anyone got an idea on how to get around this issue?</p>
  
  <p><strong>Here's an example</strong></p>
  
  <pre><code>private boolean isEditable = true;
  private EditText text;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      text = (EditText) findViewById(R.id.editText1);
      text.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void afterTextChanged(Editable s) {
          }
  
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count,
                  int after) {
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before,
                  int count) {
              if (isEditable) {
                  isEditable = false;
                  styleText(s.toString());
              } else {
                  isEditable = true;
              }
          }
  
      });
  
  }
  
  private void styleText(String completeText) {
      text.setText(completeText + " test");
  }
  </code></pre>
  
  <p>And while the above actually seems to be working, I cannot get it to work with <code>Html.fromHtml();</code>, which is what I intend to use.</p>
  
  <p><strong>EDITED AGAIN</strong></p>
  
  <pre><code>public class Main extends Activity implements TextWatcher {
  
      private EditText text;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          text = (EditText) findViewById(R.id.editText1);
          text.addTextChangedListener(this);
      }
  
      @Override
      public void afterTextChanged(Editable s) {
          text.removeTextChangedListener(this);
      }
  
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
              int after) {
          text.addTextChangedListener(this);
      }
  
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
          text.removeTextChangedListener(this);
          text.setText("Test!");
      }
  }
  </code></pre>
  
  <p>It's throwing a <code>StackOverflowException</code> on line 37, which is "<code>text.setText("Test!");</code>"</p>