4

I need to implement a wysiwyg EditText and I faced a problem at the very beginning. So I added the following code to onTextChanged:

int textLen = textWord.getText().length();
if (s.length() > textWordBefore.length()) {
    if (boldToggle == true) {
        textWord.getText().setSpan(new StyleSpan(android.graphics.Typeface.BOLD), textLen-1, textLen, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

I converted the spanned text to Html to see how it works. It works fine when I add symbols. At first it looks like this:

<b>a</b><b>b</b><b>c</b>

Then I press space and it changes to this:

<b>abc</b>

But when I press backspace the first time, only the last symbol of a word remains bold. And after one more time, the whole word becomes normal (i.e. not bold). I googled a lot, tried to re-apply a span when the backspace was pressed (if text after the change is shorter). I tried changing the span type to INCLUSIVE etc. I tried DroidWriter and it works the same way with the backspace.

It also works not in the way it is supposed to if I turn on the bold in the middle of a non-bold word and vice versa.

I really need the solution soon, I've been onto it for a few days now. Thanks in advance!

Alexander
  • 631
  • 1
  • 8
  • 19

0 Answers0