This is the code that I have.
val s = SpannableString("hello")
s.setSpan(StyleSpan(Typeface.BOLD), 0, s.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
textView.text = TextUtils.concat(s, s)
The text shown in textView is: hellohello
Why aren't both "hello" in bold?
Edit: I am actually trying to concatenate some SpannableStrings with some normal Strings, something like TextUtils.concat("A", s, "B", s, "C") (the result of which I expect to be AhelloBhelloC). Replacing SPAN_INCLUSIVE_EXCLUSIVE with SPAN_INCLUSIVE_INCLUSIVE solves the "hellohello" problem but not this one.
https://developer.android.com/reference/android/text/TextUtils#concat(java.lang.CharSequence...):
While this isn't super clear (at least to me) I infer that paragraph boundaries are extended if they should be extended. They should be extended if they are INCLUSIVE boundaries meaning if you insert at the beginning you should use
Spannable.SPAN_INCLUSIVE_EXCLUSIVEorSpannable.SPAN_INCLUSIVE_INCLUSIVE, if you append at the end, you should useSpannable.SPAN_EXCLUSIVE_INCLUSIVEorSpannable.SPAN_INCLUSIVE_INCLUSIVE.So use
Spannable.SPAN_INCLUSIVE_INCLUSIVEinstead ofSpannable.SPAN_INCLUSIVE_EXCLUSIVE(I tested it and it works).With SPAN_EXCLUSIVE_INCLUSIVE:
With SPAN_INCLUSIVE_INCLUSIVE:
After some digging I found TextUtils is using SpannableStringBuilder.append which is using SpannableStringBuilder.replace (https://developer.android.com/reference/android/text/SpannableStringBuilder#replace(int,%20int,%20java.lang.CharSequence,%20int,%20int)) under the hood and the replace is considered an insert at the end. With SPAN_EXCLUSIVE_INCLUSIVE it would not expand the formatting while it would with SPAN_INCLUSIVE_INCLUSIVE:
I also checked the resulting span and printed the start/end of the StyleSpan. The first line with SPAN_EXCLUSIVE_INCLUSIVE the last line with SPAN_INCLUSIVE_INCLUSIVE: