Android Edittext- Clearing Spans

Henry Thompson picture Henry Thompson · Feb 22, 2012 · Viewed 8.6k times · Source

I am trying to get an EditText to clear its spans by calling EditText.getText().clearSpans(). However, if I call this method, the EditText starts to behave strangely, with line feeds appearing as boxes and any spans I then set being in completely the wrong place.

So my question is: How do I clear spans from and EditText? (Without calling setText()- the text may be thousands of lines long and its too slow to redraw it all frequently)

Thanks very much!

Answer

André Restivo picture André Restivo · Apr 23, 2012

Had the same problem. Solved it by removing only the types of spans that I added to the EditText. I guess clearSpans removes more than it should. I did something like this for each type of span I used:

toRemoveSpans = et.getSpans(0, et.getText().length(), ForegroundColorSpan.class);
for (int i = 0; i < toRemoveSpans.length; i++) 
    et.removeSpan(toRemoveSpans[i]);