Android Spanned, SpannedString, Spannable, SpannableString and CharSequence
Asked Answered
E

2

114

Android offers a variety of interfaces all related to text and strings: Spanned, SpannedString, Spannable, SpannableString and CharSequence.

I have used all of the above in various scenarios usually after using Html.fromHtml() to display linkable text inside a TextView in order to apply some styling to it.

I have tried to understand the purpose/usage of these interfaces from Android's official documentation and have failed as it is quite confusing.. What is the purpose of these interfaces? in which scenarios is it mostly common to use them? In which cases is it best to avoid using them? Are there any obvious performance impacts to be considered when using any one of them?

If anyone could provide a decent explanation it would be much appreciated.

Eldridgeeldritch answered 9/7, 2013 at 11:22 Comment(0)
C
173

What is the purpose of these interfaces?

diagram from yuml.me/edit/5f8da6cb CharSequence is a standard Java interface representing a sequence of characters. String is the most commonly-used concrete implementation of CharSequence, followed by StringBuilder.

Spanned is a CharSequence with "spans" indicating formatting to apply to portions of the text, where those spans cannot be modified.

Spannable is a Spanned, adding in the ability to modify the spans (to add or remove formatting), but not to modify the text itself.

SpannedString is a concrete implementation of the Spanned interface.

SpannableString is a concrete implementation of the Spannable interface.

in which scenarios is it mostly common to use them?

When there is a method that returns one (e.g., getText() on an EditText) or when there is a method that takes one as a parameter (e.g., setText() on a TextView). diagram from yuml.me/edit/35c8f39f

Your cited case of using Html.fromHtml() is perhaps the most common in conventional Android development, as a TextView with a Spanned is much lighter in weight than is a WebView. However, there are other use cases, such as:

In which cases is it best to avoid using them?

They are singularly awful at combating baldness, snow removal, heat pump repair, making a soufflé, etc.

:-)

Are there any obvious performance impacts to be considered when using any one of them?

Interfaces, by definition, do not have "performance impacts" -- they are merely a description of an API.

I am not aware that SpannableString is significantly slower than SpannedString at any particular operation. However, SpannableStringBuilder (which allows for manipulating the text in addition to the spans that format that text) may well be a bit slower than SpannableString or SpannedString for various things. Whether or not the performance differences are enough to matter will depend on usage, though.

Charlettecharley answered 9/7, 2013 at 11:41 Comment(5)
Since the official docs are silent, How did you get these info from? Manual source browsing the hardcore way?Buffy
@Pacerier: I am not sure what part of this answer you are referring to. For example, the class hierarchy described in this answer most certainly comes from the documentation, specifically the JavaDocs. Conversely, the lack of utility of these things for combating baldness comes from personal observation.Charlettecharley
@CommonsWare, so does that mean that a string with smileys in between would be considered a spannableString and not a normal one.. because I am facing a huge lag with setText(my_string) when my_string has smileys in it (like that of watsapp) as compared to normal string..(which has only plain text) kindly throw some light... somehow I believed that smilleys are nothing but text (unicode) onlyLanderos
@eRaisedToX: Smileys could be implemented as emoji or images. AFAIK, emoji would be individual Unicode characters and presumably could be in a regular string. Images would require ImageSpan which in turn requires SpannableString.Charlettecharley
Presumably sure...but try dealing with emojis and using regular Strings and you will be right back to combating baldness. Editables and SpannableStringBuilders are your best friend when it comes to implementing emojis. I found that while yes they are simply unicode, it's how you code the identifying of the emoji span and the setting of the emoji span that is where your performance cost is coming in. Be careful using other people's emoji Libraries if you don't know how they function...Elinorelinore
A
60

String

A String is immutable (ie, the text can't change). It also doesn't have any spans associated with it. (Spans are ranges over the text that include styling information like color, highlighting, italics, links, etc.) So you can use a String when your text doesn't need to be changed and doesn't need any styling.

StringBuilder

A StringBuilder has mutable text, so you can modify it without creating a new object. However, it doesn't have any span information. It is just plain text. So use a StringBuilder when you need to change the text, but you don't care about styling it.

SpannedString

A SpannedString has immutable text (like a String) and immutable span information. It is a concrete implementation of the requirements defined by the Spanned interface. Use a SpannedString when your text has style but you don't need to change either the text or the style after it is created.

Note: There is no such thing as a SpannedStringBuilder because if the text changed then the span information would also very likely have to change.

SpannableString

A SpannableString has immutable text, but its span information is mutable. It is a concrete implementation of the requirements defined by the Spannable interface. Use a SpannableString when your text doesn't need to be changed but the styling does.

SpannableStringBuilder

A SpannableStringBuilder has both mutable text and span information. It is a concrete implementation of the requirements defined by the Spannable and Editable interfaces (among others). Use a SpannableStringBuilder when you will need to update the text and its style.

CharSequence

A CharSequence is an interface and not a concrete class. That means it just defines a list of rules to follow for any class that implements it. And all of the classes mentioned above implement it. So you can use a CharSequence when you want to generalize the type of object that you have for maximum flexibility. You can always downcast it to a String or SpannableStringBuilder or whatever later if you need to.

Arria answered 30/1, 2017 at 11:43 Comment(1)
I appreciate the way you presented your response, very easy to read and understand, thanksElinorelinore

© 2022 - 2024 — McMap. All rights reserved.