As you type on an EditText with autosuggestion enabled, the compose span, represented by the underlined text on the picture, is sectionized according to the stylespan I set on the toolbar. This is particularly helpful because otherwise the Google keyboard messes with the spans.
How can I do this?
Related
I am trying to create a single line text input in Jetpack Compose.
Since this input should have a fix width of 200.dp and is only one line, longer text is going to be cut off. Sometimes it looks like the cut off text does not even exist because the cut is made between two letters. In order to show the user that there is "more" text already typed I would prefer an ellipsis (e.g. This is a sample inpu...) effect.
I tried to use the default TextField and BasicTextField composables but there seems to be no easy solution.
Is there a way to create this ellipsis effect in Jetpack Compose?
What is the best approach in Jetpack Compose when I want text to be completely editable but also want some parts of it to be clickable?
I could use TextField. But is there a way to make some parts of it clickable? And furthermore I would also like to style the text by using AnnotatedString.
I could use ClickableText and make it somewhat editable. But I would also like to select some text while editing. This seems to be harder, on the first glance.
Or should I use none of them?
I'm creating a CharSequence with differently styled text spans(italic, bold, hyperlink, etc.,) and setting it on a TextView.
It is working as expected in all the other screens except one.
In that one screen alone, hyperlink font is working fine but not the other styled spans.
Is there any way to debug or inspect the textview to find out the spans set inside ?
As #pskink gave me the hint, I had found the way to debug the spans inside the CharSequence using TextUtils.dumpSpans()
It works cool, shows the span object with start, end positions and flags too.
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?
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?
I have a custom view that extends EditText that has ToggleButtons for rich text editing. If I allow autocomplete, which I want to do, the indicator for the current word triggers my detection for style spans.
For example on most devices the autocorrect eligible word is an underline. As you type I have a text watcher that keeps track of the current styles that are applied to the text and adds new spannables if the user toggles a style button. This ends up detecting the underline and turning the toggle on.
I can write code to check if the underline toggle was set before we found the span. (I would actually need to do this for all my styles really since some devices use a background color to indicate the current autocorrect word.) But I'm unsure what I would use to trigger turning the toggle back to off. Check if they typed space? What happens when you select a suggested word?
Has anyone done this? Is there a way to ask if the span is from autocomplete or any other notifications to know the OS drew the span?
This is an old question, but just now I had a similar problem, namely eliminating the unwanted underline before converting the spans into HTML. I found the answer in the source to TextView.
There is a method TextView#clearComposingText() that will remove all styles applied by the IME during autosuggest, preserving all other styles. You could likely call it after every user keystroke, which would remove the underlines.
Hope this helps someone.