Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 492
Roshni Avatar asked Jul 29 '14 08:07

Roshni


3 Answers

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
like image 140
meysam Avatar answered Nov 07 '22 00:11

meysam


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.

like image 22
G.T. Avatar answered Nov 07 '22 00:11

G.T.


you should have to use like this

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
like image 39
Rohit Avatar answered Nov 07 '22 00:11

Rohit