There is no need to convert a SpannableStringBuilder
to a SpannableString
when you can use it directly, as in a TextView
:
SpannableStringBuilder stuff = complex_routine_that_builds_it();
textView.setText(stuff);
As the question already showed, you could make the conversion if you absolutely had to by
SpannableStringBuilder stuff = complex_routine_that_builds_it();
SpannableString result = SpannableString.valueOf(stuff);
but you should consider why you would even need to make that conversion? Just use the original SpannableStringBuilder
.
SpannableString
vs SpannableStringBuilder
The difference between these two is similar to the difference between String
and StringBuilder
. A String
is immutable but you can change the text in a StringBuilder
.
Similarly, the text in a SpannableString
is immutable while the text in a SpannableStringBuilder
is changeable. It is important to note, though, that this only applies to the text. The spans on both of them (even a SpannableString
) can be changed.
Related