editable.getSpans is returning a StyleSpan, even when there isn't any style rendered on screen

88 views Asked by At

I am writing a RichTextEditor class, which has an inner RichTextEditorTextWatcher class. I am seeing a discrepancy from what I see on the screen, versus what I get when I call e.getSpans inside beforeTextChanged.

On the screen, I see text on the screen (in my case, a single character) that does not have any style applied to it, but the e.getSpans() call actually says that I have a bold style applied.

Is this a known Android bug?

public class RichTextEditor extends AppCompatEditText  
{

    // other code not shown

    public class RichTextEditorTextWatcher implements TextWatcher
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            if (after == 0) //deletion occurred
            {
                isDeletion = true;
                Editable e = RichTextEditor.this.getText();

                /** The next line is the problematic line!
                  * this.prevStyles returns a StyleSpan (bold) even when I don't see it on the screen for that character.
                  */
                this.prevStyles = e.getSpans(start, start+count, CharacterStyle.class); 

                for (CharacterStyle c : this.prevStyles)
                {
                    if (c instanceof StyleSpan)
                    {
                        if (((StyleSpan)c).getStyle() == Typeface.BOLD) 
                            boldButton.setChecked(true);
                        else
                            boldButton.setChecked(false);
                    }
                }
            }
            else
                isDeletion = false;
        }
    }
}
0

There are 0 answers