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.

4
  • 2
    Define normal text, there is nothing normal about your normal text example Commented May 31, 2019 at 1:22
  • ok, thanks for the help I edited. Commented May 31, 2019 at 1:26
  • 1
    Try to convert html to rtf. See this link: stackoverflow.com/questions/5922626/… Commented May 31, 2019 at 1:43
  • @JuarezJúnior that's helpful. Commented May 31, 2019 at 2:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate it, that could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.