5

I got a edittext and it has multiple coloured text like that code below. Is there any way to store text and colours and show them again? I used to use SharedPreference to store strings integers etc. but this isn`t working for Spannables.

Spannable span1 = new SpannableString("this");
Spannable span2 = new SpannableString("is");
Spannable span3 = new SpannableString("text");
span1.setSpan(new ForegroundColorSpan(Color.parseColor("#292929")), 0, span1.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span2.setSpan(new ForegroundColorSpan(Color.parseColor("#2980b9")), 0, span2.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span3.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, span3.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

edittext.setText(TextUtils.concat(span1, span2, span3));
user2638084
  • 191
  • 1
  • 5
  • 15

3 Answers3

6

You can use Html.toHtml() to get an HTML representation of the Spannable, then convert it back using Html.fromHtml(). However, you will want to test this thoroughly, as not every bit of formatting survives the round-trip.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi @CommonsWare, I have Spannable String that contains font style from assets. When using html.fromHtml the font style is missing. Is there other way to Store Spannable in sqlite?. – donmj Mar 23 '17 at 02:05
  • @donmj: You can try [my XHTML round-trip code](https://github.com/commonsguy/cwac-richedit#usage-xhtml-conversion) and see if it covers your use case. My guess is that you will need to come up with your own serialization mechanism. – CommonsWare Mar 23 '17 at 11:55
3

SpannableString is not parceable so you won't be able to save them and recreate them properly. What you can do is save the text and spans separately. The spans are parceable so they will be fine when you recreate them.

Spannable span = new SpannableString("this is text");
span.setSpan(new ForegroundColorSpan(Color.parseColor("#292929")), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span2.setSpan(new ForegroundColorSpan(Color.parseColor("#2980b9")), 5, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span3.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 8, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Save these
String text = span.subString(0, span.length());
List<Span> spans = span.getSpans(0, span.length(), Object.class);
Aswin Rajendiran
  • 3,399
  • 1
  • 21
  • 18
  • 1
    That's interesting -- I wonder why the spans are `Parcelable` but `SpannedString` is not? However, the question was for storing things persistently, in which case `Parcelable` is not that useful. – CommonsWare Jan 29 '14 at 23:58
2

According to @CommonsWare answer:

  1. You need to convert your spanned text to HTML String:
String html = Html.toHtml(yourEditText.getText());
  1. After converting spanned text to string, you can store it in database or Shared Preferences or anything else.

  2. For recover the stored string, you have to restore it and set it to your EditText:

String storedString =  //The string you saved it before 
yourEditText.setText(Html.fromHtml(storedString));

You have a spanned string now!

Mahdi Khansari
  • 321
  • 4
  • 13