Well, I try to build a rich text editor.
I have some buttons to format my editable text (bold, italic, URL, etc.).
I use Google keyboard with all text correction options enabled (Settings > Language & input > Google keyboard > Text correction).
I do the following:
In my EditText
, I write some text.
I select it and apply a bold span with SPAN_EXCLUSIVE_EXCLUSIVE
(33) as flags.
Then, I move my cursor to the end.
Finally, I add text to the end of the text. Added text should be without bold.
Okay, here is the problem. My bold span flags has changed... Why!?
Here is some logs:
D/ContentUtils: beforeTextChanged: start end span flags
D/ContentUtils: beforeTextChanged: 0 7 ChangeWatcher 8388626
D/ContentUtils: beforeTextChanged: 0 7 ChangeWatcher 6553618
D/ContentUtils: beforeTextChanged: 0 7 TextKeyListener 18
D/ContentUtils: beforeTextChanged: 0 7 SpanController 18
D/ContentUtils: beforeTextChanged: 7 7 START 546
D/ContentUtils: beforeTextChanged: 7 7 END 34
D/ContentUtils: beforeTextChanged: 0 7 SpellCheckSpan 33
D/ContentUtils: beforeTextChanged: 0 7 CustomBoldSpan 33
D/ContentUtils: onTextChaghed
D/ContentUtils: onTextChaghed: 0 8 ChangeWatcher 8392722
D/ContentUtils: onTextChaghed: 0 8 ChangeWatcher 6557714
D/ContentUtils: onTextChaghed: 0 8 TextKeyListener 4114
D/ContentUtils: onTextChaghed: 0 8 SpanController 4114
D/ContentUtils: onTextChaghed: 8 8 START 546
D/ContentUtils: onTextChaghed: 8 8 END 34
D/ContentUtils: onTextChaghed: 0 8 CustomBoldSpan 4129
D/ContentUtils: onTextChaghed: 0 8 UnderlineSpan 289
D/ContentUtils: onTextChaghed: 0 8 ComposingText 289
D/ContentUtils: afterTextChanged
D/ContentUtils: afterTextChanged: 0 8 ChangeWatcher 8392722
D/ContentUtils: afterTextChanged: 0 8 ChangeWatcher 6557714
D/ContentUtils: afterTextChanged: 0 8 TextKeyListener 4114
D/ContentUtils: afterTextChanged: 0 8 SpanController 4114
D/ContentUtils: afterTextChanged: 8 8 START 546
D/ContentUtils: afterTextChanged: 8 8 END 34
D/ContentUtils: afterTextChanged: 0 8 CustomBoldSpan 4129
D/ContentUtils: afterTextChanged: 0 8 UnderlineSpan 289
D/ContentUtils: afterTextChanged: 0 8 ComposingText 289
D/ContentUtils: afterTextChanged: 0 8 SpellCheckSpan 33
When I use another keyboard, all went fine.
When I disabled Text correction settings, all went fine.
All of my span are custom span and subclass an existing Android span.
It seems Google Keyboard modify my spans by its own (probably because of Show suggestions
settings).
How can I avoid this?
Maybe I am missing something about span flags?
Add phrases and shortcuts to GBoardGo to GBoard Settings >> Dictionary>>Personal dictionary>> Select keyboard and tap on '+' to add custom phrase and shortcuts. You can also access settings menu directly by long-pressing comma key.
Ok, after some research, it seems the keyboard apply some spans around a word while typing in order to manage suggestions.
Problem is for each typed letter, the word is removed and added back with the added letter. At this point I loose some custom spans, like those in the middle of the word.
If you add a TextWatcher to your EditText, it will be called 2 times: first with the added letter, second after removed and added back the whole word. Not convenient at all.
So, an ugly solution is to copy all spans during beforeTextChanged()
and apply back during the second afterTextChanged()
. But it is complicated to implement.
Anyway, other apps do not do better: GMail and Evernote have same kind of issues. I choose to not worry and do not apply the ugly solution. My rich text editor is usable like this...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With