Html string with <style> tag and style in android textview

20,464

Solution 1

for html you must use this :

mytext.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

also check this link http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29

and This is a list of allowed HTML tags:

br
p
div
em
b
strong
cite
dfn
i
big
small
font
blockquote
tt
monospace
a
u
sup
sub

Solution 2

The style and span tags are not supported in TextView.

What you can do is this:

String myHtmlString = Html.fromHtml("<u> <font color=\"#FF0000\"> some text </font> </u>");

FYI, here is a list of supported tags.

Solution 3

you should have to use like this

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
Share:
20,464
Roshni
Author by

Roshni

Updated on December 21, 2020

Comments

  • Roshni
    Roshni over 3 years

    I'm trying to show display an html string in a textview. I used the Html.fromhtml method to load html string textview. But It failed to parse tag. Here is my html string

    <style>u.style{color:#FF0000;}span.style2{color:#000000;}</style>
    <u class="style"><span class="style2">some text</span></u>
    

    I had tried Spannable String also, But It displays underline and underlined text in same color.I don't want to change text color.

    s.setSpan(new ForegroundColorSpan(Color.CYAN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    Can anyone help me to create custom textview, which can support tag in textview?