1

I'm trying to apply ForegroundColorSpan with Spannable.EXCLUSIVE_INCLUSIVE flag when using TextWatcher at a specific index range.

With my device's default input (Samsung Keyboard) there are no issues and everything works as expected, however when using Google Keyboard as input that's a different story.

From my debugging I found that Google Keyboard places whole words, even if you change one character - I guess that's because of the word correction feature. As a result, if the Span was applied at some point in the word (even at the end), and a user enters more text, the span disappears. The only exception is when the span is applied to the last character in the word, and the user enters a space ("ends" the word).

The workaround I found is to simply check if the input is a whole word, and whenever it is - apply the Span again. While that does work, it's not a very good solution since I need to set the caret at the end every time, and the fact that I have to re-apply the spin when it could be avoided (that's why I use Spannable.EXCLUSIVE_INCLUSIVE flag).

P.S. I tried other flags such as INCLUSIVE_INCLUSIVE and it sorta works - however it applies the span backwards as-well, resulting the Span to be applied to the entire word, rather than the index I specified.

Are there better solutions to this issue?

user3705007
  • 265
  • 4
  • 12

1 Answers1

2

Okay so I found a different solution - turning off keyboard's input suggestion. Doing so prevents the input from being whole word and instead just a single character every time.

To do so I had to add textNoSuggestions flag to EditText's inputType attribute.

android:inputType="textNoSuggestions|<other_flags>"

I found more information about this inputType here: Turn off autosuggest for EditText?

Community
  • 1
  • 1
user3705007
  • 265
  • 4
  • 12