3

As I am getting the html text from the service and I need to display the text on text View.

Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>

I am setting this text like

tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>"));

It is showing bold but not showing the color red text.

Ahmad Arslan
  • 4,498
  • 8
  • 38
  • 59
  • Might be a problem with your quotes. Try using single quotes inside your .fromHTML('...'); method. – Trey Shaffer Jan 12 '15 at 07:11
  • http://stackoverflow.com/questions/10392163/using-multiple-text-colors-in-androids-textview-html-fromhtml – PPD Jan 12 '15 at 07:12

4 Answers4

6

As @ρяσѕρєя K told above that HTML span tag is not supported by Html.fromHtml.

You should either change the service
OR
add a following line to your code, it will change the span color html to font tags, atleast in your case.

String yourHtmlText = yourHtmlText.replace("span style=\"color:", "font color='").replace(";\"","'").replace("</span>", "</font>");  

For others I'll recommend to use String.split frequently according to your needs and it will work like magic.

I hope this works.
Cheers :)

Salmaan
  • 3,543
  • 8
  • 33
  • 59
4

As you can see HTML Tags Supported By TextView HTML span tag is not supported by Html.fromHtml.

So you should return only supported tags from server like font,div,p,... or use webview to show all html tags

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

you have to use following code to show data from html

tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <font color='red'>simple</font>"));
Anjali
  • 1
  • 1
  • 13
  • 20
  • the string I am getting from the service All I have to do is to display the text. – Ahmad Arslan Jan 12 '15 at 07:15
  • 1
    http://stackoverflow.com/questions/12285515/way-to-support-span-tag-in-textview span is not support in android either you have to ask the server end developer to send font tag or you have to change the span to font in your end – Anjali Jan 12 '15 at 07:19
1

enter text seperately

TextView text = ... // find or instantinate your text view.
text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));

or use spannable string

text.setText("");
text.append("Add all your funky text in here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);

use this library support all html tags.

https://github.com/NightWhistler/HtmlSpanner

Dinesh Raj
  • 664
  • 12
  • 30