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
Share:
28,012
Eugene
Author by

Eugene

Updated on January 11, 2021

Comments

  • Eugene
    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
    Ljdawson about 12 years
    If you look at the method signature, it takes CharSequence... which means it will merge as many as you give it.
  • Madbreaks
    Madbreaks over 11 years
    Sometimes, not very often, Java makes things easy. I love those times. +1
  • Display Name
    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
    marwinXXII almost 10 years
    In 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
    Muhammad Alfaifi over 9 years
    Same thing here first spannable loses styling only the second one maintains its style!
  • minipif
    minipif almost 9 years
    If your first span style disappeared, it could be because you've used the same StyleSpan on both spans. You need to create a new StyleSpan for each span. (see this answer)
  • HaydenKai
    HaydenKai almost 8 years
    @marwinXXII the " " is a String, which works with the CharSequence interface however it is not a Spannable. new SpannableString(" ") would work in place of " "
  • nulldev
    nulldev over 6 years
    @marwinXXII I've found a workaround for this in my answer: stackoverflow.com/a/49080221/5054192
  • sud007
    sud007 about 4 years
    Works just fine! Thanks!