title: Remove whole Spannable with backspace
body: <p>I have a chat application which offers the possibility to add emoticons to the text.</p>
  
  <p>I have a problem with the <code>EditText</code> field. The emoticon images show up but if I press on the normal keyboard the backspace button, the text which I was changing to an emoticons picture shows up and I have to remove several characters until the picture goes away. I am using <code>Spannable</code> to do this. </p>
  
  <p>I want the whole smilie go away if the user presses one time backspace. </p>
  
  <p>Here the code I am using: </p>
  
  <pre><code>// This is in the keyclicked listener
  {
      ...
      smilie = "(angel)";
      break;
      ...
      int cursorPosition = content.getSelectionStart();
      content.getText().insert(cursorPosition, getSmiledText(this, smilie));
      content.getText().insert(cursorPosition + smilie.length(), " ");
  }
  
  public static boolean addSmiles(Context context, Spannable spannable) {
      boolean hasChanges = false;
      for (Entry&lt;Pattern, Integer&gt; entry : smilies.entrySet()) {
          Matcher matcher = entry.getKey().matcher(spannable);
          while (matcher.find()) {
              boolean set = true;
              for (ImageSpan span : spannable.getSpans(matcher.start(),
                      matcher.end(), ImageSpan.class))
                  if (spannable.getSpanStart(span) &gt;= matcher.start()
                          &amp;&amp; spannable.getSpanEnd(span) &lt;= matcher.end())
                      spannable.removeSpan(span);
                  else {
                      set = false;
                      break;
                  }
              if (set) {
                  hasChanges = true;
                  spannable.setSpan(new ImageSpan(context, entry.getValue()),
                          matcher.start(), matcher.end(),
                          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
              }
          }
      }
      return hasChanges;
  }
  
  public static Spannable getSmiledText(Context context, CharSequence text) {
      Spannable spannable = spannableFactory.newSpannable(text);
      addSmiles(context, spannable);
      return spannable;
  }
  </code></pre>