Ahmad Arslan
Ahmad Arslan

Reputation: 4528

Set Span style HTML text in Text View android

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.

Upvotes: 3

Views: 17435

Answers (4)

Dinesh Raj
Dinesh Raj

Reputation: 654

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

Upvotes: 1

Salmaan
Salmaan

Reputation: 3624

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 :)

Upvotes: 6

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

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

Upvotes: 5

Anjali
Anjali

Reputation: 2535

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

Upvotes: 2

Related Questions