Bixms
Bixms

Reputation: 811

"How to display div html content in android on textview

I have value on string:

String htmlValue = "<div style="text-align: center;"><span style="font-family: impact, chicago; font-size: 18px;">Exchange Offer !!&nbsp;</span></div>
    <div style="text-align: center;"><span style="font-size: 14px;">any mobile phone</span> , <span style="font-size: 14px;">give your</span> <span style="font-family: impact, chicago; font-size: 20px;">take new one</span>.</div>";

and how to display such type div included in textview.

I have try this but only content display div styling could not display.

Spanned result= Html.fromHtml(htmlValue);
tvFooter.setText(result);

or

 tvFooter.setText(Html.fromHtml(htmlValue));

Here Defined: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html , div supported but could get exact expected output by doing above. I have both of above trick, but not working.

Error Output:

enter image description here

Expected Output like this

:enter image description here

Upvotes: 1

Views: 4301

Answers (3)

yummy
yummy

Reputation: 580

As I scanned the source code of /frameworks/base/core/java/android/text/Html.java , the attribute style and the tag span are not supported. Suggest using webview instead of textview

Following tags or attributes seems to be supported in Lollipop,

 - <br>
 - <p>
 - <div>
 - <strong>
 - <b>
 - <em>
 - <cite>
 - <dfn>
 - <i>
 - <big>
 - <small>
 - <font color="..." face="...">
 - <blockquote>
 - <tt>
 - <monospace>
 - <a href="...">
 - <u>
 - <sup>
 - <sub>
 - <h1>
 - <h2>
 - <h3>
 - <h4>
 - <h5>
 - <h6>
 - <img src="...">

Upvotes: 2

Chandra Sharma
Chandra Sharma

Reputation: 1340

All html tags are not supported by Html.fromHtml() check here the supported tags in textview.So better you use webview instead of textview to load the html content. like

 yourwebview.loadData(yourhtmlData, "text/html", "UTF-8");

Upvotes: 0

Revi
Revi

Reputation: 9

You need to write \ before inside " because they're closing your string before they properly end, like Jemshit Iskenderov says.

Also, the tags style and span can't be shown in a textview. Check this link for a list of HTML tags that work in textview.

Upvotes: 1

Related Questions