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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With