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.
Related
I have a html string that I want to display in TextView. The html string has css inline style and also css in header. I would like to ask if I can display the html string with css in TextView?
I tried Html.fromHtml(), but the css doesn't apply.
Android native TextView doesn't support all HTML tags and features. It only does support a few tags and properties. So, in your case, there's a couple of options which you may want to consider.
Use WebView to show rich texts. However, WebViews are too heavy and slow.
Use 3rd party libraries that improve native TextView and support more HTML tags. For example: HTML-TextView.
Android does not support CSS in TextView and has a limited Html markup support. Use WebView instead.
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.
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.
I have some data which was formatted using MathJax on WebApplication. Now, I want to display those Data in Android App and that is in TextView only.I have already downloaded MathJax from Leathrum's WordPress
I found some examples but all are using WebView. But I want it using TextView. How to get this ???
I am also using ImageGetter with that TextView and it is working fine. Now, just want to do something for MathJax.
Why only TextView ???
My data contains simple text, html text, images, and math functions... I was using webview but it is showing extra space at bottom and also have using with Webview because I am using Custom ListView with WebView
But I want it using TextView. How to get this ?
You don't. TextView is not designed for this. Your choices are:
Stick with WebView
Figure out how to render your mathematical expressions to images, then either use an ImageView, ImageSpan, etc.
Write your own widget that draws the mathematical expression to a Canvas
MathJax is a cross-browser JavaScript library that displays mathematical notation in web browsers, using MathML, LaTeX and ASCIIMathML markup.
Source: MathJax in Wiki
The very first line has an answer for you. It's a cross-browser javascript library. you can not use it for textviews. Doesn't it make sense ?
Update:
Solution 1: You could write a mapper which will have mapping of each Math symbols to its html code and use it to showcase on your TextView based on need.
Solution 2: There is a nice open source: MathView Which is using MathJax internally that you can use.
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.