How to merge some spannable objects?
28,012
Solution 1
You could use this:
TextUtils.concat(span1, span2);
http://developer.android.com/reference/android/text/TextUtils.html#concat(java.lang.CharSequence...)
Solution 2
Thanks, it works. I have noticed that I can merge even 3 spannable object:
(Spanned) TextUtils.concat(foo, bar, baz)
Solution 3
I know this is old. But after modifying kotlin stdlib a bit I've got this code:
fun <T> Iterable<T>.joinToSpannedString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): SpannedString {
return joinTo(SpannableStringBuilder(), separator, prefix, postfix, limit, truncated, transform)
.let { SpannedString(it) }
}
Hope it might help somebody.
Solution 4
Use SpannableStringBuilder
.
Even better- make a kotlin operator overload:
operator fun Spannable.plus(other: Spannable): Spannable{
return SpannableStringBuilder(this).append(other)
}
just throw that in any kotlin file as a top level function.
and the you can concatenate using +
:
val spanA = ...
val spanB = ...
val concatenatedSpan = spanA + spanB
Author by
Eugene
Updated on January 11, 2021Comments
-
Eugene over 3 years
I divide a spannable object into 3 parts, do different operations, and then I need to merge them.
Spannable str = editText.getText(); Spannable selectionSpannable = new SpannableStringBuilder(str, selectionStart, selectionEnd); Spannable endOfModifiedSpannable = new SpannableStringBuilder(str, selectionEnd, editText.getText().length()); Spannable beginningOfModifiedSpannable = new SpannableStringBuilder(str, 0, selectionStart);
How can I do it? I haven't found the required method or constructor to do it.
-
Ljdawson about 12 yearsIf you look at the method signature, it takes CharSequence... which means it will merge as many as you give it.
-
Madbreaks over 11 yearsSometimes, not very often, Java makes things easy. I love those times. +1
-
Display Name almost 10 years@Madbreaks this is not related to Java, but to Android API. It could be made easy in C++ as well.
-
marwinXXII almost 10 yearsIn my case this method had some problems:
TextUtils.concat(span1, " ", span2);
style (ie span) for span1 disappeared, only it's string representation left. Wrapping " " into Spanned didn't help. -
Muhammad Alfaifi over 9 yearsSame thing here first spannable loses styling only the second one maintains its style!
-
minipif almost 9 yearsIf your first span style disappeared, it could be because you've used the same
StyleSpan
on both spans. You need to create a newStyleSpan
for each span. (see this answer) -
HaydenKai almost 8 years@marwinXXII the
" "
is aString
, which works with theCharSequence
interface however it is not aSpannable
.new SpannableString(" ")
would work in place of" "
-
nulldev over 6 years@marwinXXII I've found a workaround for this in my answer: stackoverflow.com/a/49080221/5054192
-
sud007 about 4 yearsWorks just fine! Thanks!