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.
Related
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 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 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]*)<""", "")
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. :)
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.