android SPAN_EXCLUSIVE_EXCLUSIVE not working properly
Asked Answered
W

2

8

I am trying to set span on a SpannableStringBuilder using flag SPAN_EXCLUSIVE_EXCLUSIVE and I am facing problem on further editing the text to which I am setting span.

Expected behaviour 1: Original text. 2: Text added before. 3: Text added after with space.

Unexpected Behaviour on adding text after styled text

I don't want the added text to be styled, and want to know what am I doing wrong.

EDIT 1: The issue is happening on Moto X Play, but is not reproduced on Nexus 5X. Still testing on other devices.

Wellread answered 22/5, 2017 at 6:14 Comment(0)
P
0

You just probably add text not the way you should. Use .insert() and .append() methods of SpannableStringBuilder to add additional text.

I just tried what you try to achieve and here is the result:

TextView hratkyTextView = (TextView) findViewById(R.id.spannableHratkyTextView);

final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold

// "Test text" part (in bold)
SpannableStringBuilder builder = new SpannableStringBuilder("Test text");
builder.setSpan(bss, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// Prepending "before" (non-bold)
builder.insert(0, "before");

// Appending " after_with_space" to the end of the string
builder.append(" after_with_space");

hratkyTextView.setText(builder);

Result: Nexus 7 Emulator running MainActivity with this code

Painless answered 22/5, 2017 at 7:15 Comment(2)
Not changing text via code, it was an EditText and the text changes were made by typing.Wellread
+1 @Wellread I'm experiencing the same issue from user's inserted chars after span. Also, please note that the span in unexpectedly expanding only when inserting without space, e.g. Test text after_with_space works but not Test textafterLeaseholder
L
0

TL;DR: Using some IMEs, like Gboard, when adding a char directly after a word (without space) the IME will replace the whole word tric with trick instead of just appending the c.

Detailed asnwer: How IMEs work with editors.

How some IMEs dictate commands to editors IMEs communicate with editors (e.g. EditText) through InputConnection interface where they can send commands following user input, and get current text.

Gboard IME works in the following way:

  • gets text before and after cursor
  • detects the currently "composing" word and asks the editor to highlight and remember it (usually results in the word being underlined - check screenshot below)

Being aware of the currently composing word enables many features like suggesting words or auto-correcting spelling.

  • Whenever a char is inputted by the user, Gboard will ask the editor to set the currently composing text to a new value, i.e. replace trick by tricky
  • After a space is inputted, Gboard will do a final replace of currently composing region, eventually auto-correcting spelling
  • Currently composing region is reset to the next word.

This unfortunately breaks what we would normally expect from SPAN_EXCLUSIVE_EXCLUSIVE.

screenshot with Gboard underlining the currently composing region

Leaseholder answered 12/7, 2021 at 13:10 Comment(1)
How can we make Exclusive spans to work ? Any ideaEarthward

© 2022 - 2024 — McMap. All rights reserved.