Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.fromHtml not working for span

I have a TextView and I want to show multi-colored text in that. I am using html.fromHtml for this, but it is not showing any color.

My code is:

//statusToShow is a String
Log.d("html ==>", statusToShow) ;
RStatus.setText(Html.fromHtml(statusToShow),
TextView.BufferType.SPANNABLE);

Value from Log is:

<span style='background-color:#80B5FF;color:#fff'>C</span> <span style='background-color:#CF8DEA;color:#fff'>P</span> <span style='background-color:red;color:#fff'>E</span> <span style='background-color:green;color:#fff'>D</span>

like image 698
Mr_Hmp Avatar asked Mar 23 '23 21:03

Mr_Hmp


2 Answers

Not all tags are supported, list of supported tags is here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

like image 87
be4code Avatar answered Mar 25 '23 10:03

be4code


Try using Spannable like this:

Spannable WordtoSpan = new SpannableString("Text To Span");

WordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), 0, 4,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);

This will make the background color of Text Blue.

Hope this helps.

like image 43
R4chi7 Avatar answered Mar 25 '23 11:03

R4chi7