0
votes

I have a SpannableStringBuilder object that applies some formatting to its text. Now I'd like to insert another formatted string into this object. I can see that SpannableStringBuilder has an insert() method, but it only accepts a CharSequence which doesn't allow me to apply any text formatting.

So I am wondering if it is possible to insert a SpannableStringBuilder object into another SpannableStringBuilder object in any way or do I have to rebuild the complete SpannableStringBuilder object if I need to insert formatted text into it?

1

1 Answers

0
votes

You mean that both the destination and source SpannableStringBuilders have spans attached, right?

SpannableStringBuilder.replace() - which insert() uses internally - says

If source is Spanned, the spans from it are preserved into the Editable

so it should work.

However, it only seems to work if dest does not already contain the span that source is trying to insert. See this comment:

// Add span only if this object is not yet used as a span in this string

in SpannableStringBuilder.java

I'm not sure why its been implemented with this constraint.