Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spannable text save/store issue

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));
like image 735
user2638084 Avatar asked Jan 29 '14 23:01

user2638084


1 Answers

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.

like image 159
CommonsWare Avatar answered Sep 27 '22 19:09

CommonsWare