Suragch
Suragch

Reputation: 511736

Android StringBuilder vs Editable

While trying to comprehend this answer, I failed to understand the difference between StringBuilder and Editable. This image (minus the hand-drawn red circles) comes from that answer.

enter image description here

Both of them have methods to append, delete, insert, and replace text. I see that StringBuilder is an actual class while Editable is an interface. But I can get an Editable back from a TextView with textView.getEditableText(), which seems strange if it is an interface. What is the concrete implementation behind the scenes? I also see that Editable can have spans while a StringBuilder doesn't.

I think I am close to understanding the difference, but I could use a little more explanation. I was surprised that I couldn't find where this question had already been asked, so I am asking it now.

Again, specifically:

Upvotes: 2

Views: 408

Answers (1)

Blackbelt
Blackbelt

Reputation: 157447

What is the difference between StringBuilder and Editable?

Editable can hold markups such html. StringBuilder holds only plain text.

But I can get an Editable back from a TextView with textView.getEditableText(), which seems strange if it is an interface. What is the concrete implementation behind the scenes?

It is not strange. It returns an Editable, the concrete implementation depends on how you set the text.

is the object that I am getting back from TextView actually a SpannableStringBuilder? And if so, why do I get an incompatible types error if I write SpannableStringBuilder myString = textView.getEditableText();

It might be or it might be not. What you know is that is an Editable. A SpannableStringBuilder is an Editable, but an Editable is not a SpannableStringBuilder, that's why you can't assign the return value of getEditableText to a SpannableStringBuilder unless you down cast it. I

Upvotes: 1

Related Questions