Set Span style HTML text in Text View android
Solution 1
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 :)
Solution 2
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
Solution 3
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>"));
Solution 4
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.
Related videos on Youtube
Ahmad Arslan
Updated on September 15, 2022Comments
-
Ahmad Arslan over 1 year
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.
-
Trey Shaffer over 9 yearsMight be a problem with your quotes. Try using single quotes inside your .fromHTML('...'); method.
-
PPD over 9 years
-
-
Ahmad Arslan over 9 yearsPlease read my question.. I just wanted to display the text which I am getting from the server.