How to use removeSpan() on android textview?

android, html, java

Solution

Get the text of the `TextView` and cast it to `SpannableString` then you can use the `getSpans(int queryStart, int queryEnd, Class<T> kind)` method to iterate over those spans and remove them

SpannableString ss=(SpannableString)txtView.getText();
ForegroundColorSpan[] spans=ss.getSpans(0, txtView.getText().length(), ForegroundColorSpan.class);
for(int i=0; i<spans.length; i++){
  ss.removeSpan(spans[i]);
}

Problem

Pardon me if I'm asking a dumb question but I would like to know how to remove a span from text in my textview. This is how my span method looks like. ``` public CharSequence setTextStyleItalic(CharSequence text) { StyleSpan style = new StyleSpan(Typeface.ITALIC); SpannableString str = new SpannableString(text); str.setSpan(style, 0, text.length(), 0); return str; } ``` and this is how i call it. ``` tvTitle.setText(setTextStyleItalic(tvTitle.getText())); ``` I would really like to know how to remove this italic span in java using removeSpan() please.

Original source