Android Spannable text save/store issue

android, android-edittext, spannable, store

Solution

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.

Problem

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)); ```

Original source