0

I'm building an app using Xamarin Android, and I want to convert HTML to normal with formatting, for example :

HTML Code

<p><strong>Lorem ipsum</strong> is placeholder text <strong><em><span style="color:#ff0000">commonly</span></em></strong> used in the graphic, print, and publishing industries for previewing layouts and visual mockups.
</p>
<p>&nbsp;</p>
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

Text

Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.

  • Item 1
  • Item 2
  • Item 3

I get this content from the database and I want to convert post content to Text with formatting.

Fatihi Youssef
  • 411
  • 1
  • 6
  • 15

1 Answers1

1

The TextView currently supports the following HTML tags as listed in this blog post:

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

If you just want to display it in a TextView then simply do something like this:

TextView txtView;
txtView.TextFormatted = Html.FromHtml(HTMLFromDataSource);

If you want to use a different control then there are other ways to achieve this, but the TextView supports HTML to a degree anyway so if you can use that, I would.

However it is worth noting that UL and LI doesn't look to currently be supported. So you would have to use something like the Html.TagHandler to tell it what to do, here is a Java implementation:

public class UlTagHandler implements Html.TagHandler{
    @Override
    public void handleTag(boolean opening, String tag, Editable output,
                          XMLReader xmlReader) {
            if(tag.equals("ul") && !opening) output.append("\n");
            if(tag.equals("li") && opening) output.append("\n\t•");
    }
}

textView.setText(Html.fromHtml(myHtmlText, null, new UlTagHandler()));

You should be able to convert that to C# for Xamarin.

JoeTomks
  • 3,243
  • 1
  • 18
  • 42