1

I have an EditText, and can add formatting such as bold, italic....but how can I remove it? I've looked into getSpans, filters, and other non-string things and haven't been able to make sense of them! Ideally, I'd like to be able to clear specific tags and all tags set around the selected text.

Update with my solution:

private String getSelectedText(){
        int start = Math.max(mText.getSelectionStart(), 0);
        int end = Math.max(mText.getSelectionEnd(), 0);
        return mText.getText().toString().substring(Math.min(start, end), Math.max(start, end));
    }
private void clearFormat(){
        int s1 = Math.max(mText.getSelectionStart(), 0);
        int s2 = Math.max(mText.getSelectionEnd(), 0);
        String text = getSelectedText(); if(text==""){ return; }
        EditText prose = mText;
        Spannable raw = new SpannableString(prose.getText());
        CharacterStyle[] spans = raw.getSpans(s1, s2, CharacterStyle.class);
        for (CharacterStyle span : spans) {
            raw.removeSpan(span);
        }
        prose.setText(raw);
        //Re-select
        mText.setSelection(Math.min(s1,s2), Math.max(s1, s2));
    }
Ael
  • 323
  • 4
  • 14

3 Answers3

4

but how can I remove it?

Call removeSpan() on the Spannable.

For example, this method from this sample project searches for a search string in the contents of a TextView and assigns it a background color, but only after removing any previous background colors:

private void searchFor(String text) {
    TextView prose=(TextView)findViewById(R.id.prose);
    Spannable raw=new SpannableString(prose.getText());
    BackgroundColorSpan[] spans=raw.getSpans(0,
                                             raw.length(),
                                             BackgroundColorSpan.class);

    for (BackgroundColorSpan span : spans) {
      raw.removeSpan(span);
    }

    int index=TextUtils.indexOf(raw, text);

    while (index >= 0) {
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
          + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }

    prose.setText(raw);
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, this set me on the right track! With just a few adjustments needed :) – Ael Aug 16 '13 at 06:12
  • how do i remove a particular tag using remove span ? – Kutbi Sep 06 '16 at 09:34
  • @Kutbi: I do not know what a "tag" is here. You may wish to ask a separate Stack Overflow question, where you can explain **in detail** what you are seeking. – CommonsWare Sep 06 '16 at 11:03
0

what you could try is:

1- Create a custom style where your EditText will have "such as bold, italic..."

2- Be aware of using R.style.normalText to change it back to it's normal style at runtime

3- Change this styles depending on the behaviour you want to achieve via setTextAppearance(Context context, int resid)

Here is an example i found googling How to change a TextView's style at runtime

Edit: as your question is "How to clear formatting from an EditText" here is the specific answer as code:

editTextToClearStyle.setTextAppearance(this,R.style.normalText);
Community
  • 1
  • 1
jac1013
  • 408
  • 3
  • 11
0

Please see the comment of the snippet below.

if (makeItalic) {
    SpannableString spanString = new SpannableString(textViewDescription.getText());
    spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
    this.textViewDescription.setText(spanString);
} else {
    SpannableString spanString = new SpannableString(
        textViewDescription.getText().toString()); // NOTE: call 'toString()' here!
    spanString.setSpan(new StyleSpan(Typeface.NORMAL), 0, spanString.length(), 0);
    this.textViewDescription.setText(spanString);
}

... just get the raw string characters by calling the toString() method.

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68