Html.fromHtml does not removes <!-- comments --> - android

I am currently parsing some HTML code and I would like to put in a Textview. (Webview is too much, I just want something very simple).
The Html.fromHtml(mySource) is great and working fine.
This line makes my text turn in colors, bold, italic, etc...
Unfortunately, it has some inconvenients, it does not remove the comments.
Do you have any solution other than using a WebView?

Remove the comments yourself before passing the string to Html.fromHtml(). Whether you use a regular expression or an HTML parser is up to you, though this gentleman has an opinion on the matter.

Related

Android: Html.fromHtml() method not working with standard Html tags

So I have an HTML string I want to display as styled text in a textview but it never seems to work. My HTML string uses tags which are supposed to be supported by the fromHtml() method but it never displays the text properly, just returns the original HTML string. Here's an example of my HTML:
<div class="className"><p>Sample text http://www.example.com</p></div>
Nothing special as you can see, yet it never parses it correctly. Could it be that the method doesn't support the "class" attribute? If so, how do I add support for it?
Html class provides limited to support of HTML elements. And "class" attributes are not supported.
To understand which tags can be used and how they will be rendered refer to source code (android 5.0.1 r1). You can provide custom TagHandler or use WebView to display complex HTML page.
Give a try to this lib: https://github.com/SufficientlySecure/html-textview
It supports several tags more than native Html.fromHtml in Android.

Android - Html.fromHtml() with custom fonts

I'm trying to assign HTML text into a TextView. Here's a sample of the HTML:
<font face="myCustomFont1">Some Text</font>
<font face="myCustomFont2">Some More Text</font>
I'm using Html.fromHtml(myHtmlString) to do this.
If the fonts are system fonts, such as sans-serif-xxx, then everything works fine. The problem is using custom .ttf / .otf files which I add to my application.
Is there a way to make fromHtml recognise custom fonts? I couldn't find anything online other than this link: Using Html.fromHtml to set custom Typeface (look at the 2nd answer - inside the comments)
but it leaves the issue without an answer.
Is there a way to make fromHtml recognise custom fonts?
Not for the syntax that you are proposing. You are welcome to fork the Html class and add that in. Given the way that Html is implemented, you aren't going to be able to enable this via a subclass, AFAICT.
Using a TagHandler, you may be able to pull this off without forking Html, but a TagHandler is only going to be invoked for an unrecognized HTML tag. Since <font> is recognized, the TagHandler won't be invoked for it. If you are in control over the HTML, though, you could create a <gil> tag and use that as a <font> equivalent that routes through a TagHandler that you provide. I haven't used TagHandler, which is the reason as to why I hedge as to whether this can work.

How can I remove content from <style> tags using Html.fromHtml()?

I need to get "plain text" from html strings I'm using Html.fromHtml that works pretty good, but I notice that sometimes HTML has tags and although clean the tag well, doesn't remove the content.
I think this behavior has sense, but doesn't fit my needs. Do you know how can I remove the html inside tags? Do I have some extra string processing? (like substrings, regexp or so...).
I read about TagHandler, but I'm not sure that could solve my problem, seems something to handle additional tags not to remove content for a specific tag.
you can try replace it, something like
replaceAll(""">([a-Z0-9]*)<""", "")

HTML android in android

I want to develop an editor which can make the text bold, italics and underline and create an equivalent HTML for it. I have somethhing like this in my mind:
When the user clicks on save, I should get HTML data.
For example , if user writes:Hello World
then I should get <!Doctype HTML><body>Hello <b><i>World</i></b></body></html>
What are my options here? are there any previous projects like this or do i have to make this from scratch?
You can use Character Style sub-classes of Android for the styling of your texts. Then use HTML class to get the HTML equivalent of your text. I'm currently developing one and those are the ones I used. You may also check out https://github.com/commonsguy/cwac-richedit for an example on how you could use Character Style for editing. :)

Android: Render HTML not supported by Html.fromText

If I have html with tags not supported as part of Html.fromText I understand a WebView will do the trick as it supports standard HTML as a browser but its performance is really slow to my liking. So if I don't want to use WebView, the only other way is to parse the HTML myself and put different tags in muliple TextView inside a vertical LinearLayout?
You'll have to use Html.fromHtml() with an Html.TagHandler to handle unknown tags like <pre/> and <code/>.
See this answer for an example of how to implement Html.TagHandler.
Use replaceAll() to convert the <pre> and <code> tags to <tt>, which is supported by Html.fromHtml(). Then, use Html.fromHtml() to create the SpannedString that you pass to the TextView.

Categories

Resources