I get a special html code:
< ;p > ;This is < ;a href=" ;http://www.test.hu" ;> ;a test link< ;/a> ; and this is & ;nbsp;a sample text with special char: & ;#233;va < ;/p> ;
(There isn't space before ; char, but if I don't insert space the stackoverflow format it)
It's not a normally html code, but if I paste in a empty html page, the browser show it with normal tags:
<i><_p_>This is <_a_ href="http://www.test.hu">a test link<_/a_> and this is a sample text with special char: éva <_/p_>
</i>
This code will be shown in a browser:
This is a test link And this is a sample text with special char: éva
So I want to get this text, but I can't use Html.fromHtml
, because the component what I use doesn't support Spanned
. I wanted to try StringEscapeUtils
, but I couldn't import it.
How can I replace special chars and remove tags?
If you just want to display some html text and don't really need a TextView , then take a WebView and use it like following: String htmlText = ...; webview. loadData(htmlText , "text/html; charset=UTF-8", null); This does not restrict you to a few html tags either.
Coding? Yes, that's right—coding on your Android device is not only possible, but also popular. The top HTML editors in the Google Play Store have been downloaded millions of times, proving both professionals and enthusiasts increasingly view the operating system as a viable productivity platform.
fromHtml. Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).
↳ android.text.SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached. For mutable text, see SpannableStringBuilder .
I guess I am too late to answer Robertoq's question, but I am sure many other guys are still struggeling with this issue, I was one of them.
Anyway, the easiest way I found is this: In strings.xml, add your html code inside CDATA, and then in the activity retrieve the string and load it in WebView, here is the example:
in strings.xml:
<string name="st1"><![CDATA[<p>This is <a href="http://www.test.hu">a test link</a> and this is a sample text with special char: éva </p>]]>
</string>
you may wish to replace é with é ; (note: there is no space between é and the ; )
Now, in your activity, create WebView and load string st1 to it:
WebView mWebview = (WebView)findViewById(R.id.*WebViewControlID*);
mWebview.loadDataWithBaseURL(null, getString(R.string.st1), "text/html", "utf-8", null);
And horraaa, it should work correctly. If you find this post useful I will be greatful if you can mark it as answered, so we help other struggling with this issue
Write a parser, no different than you would in any other situation where you have to parse data.
Now, if you can get it as ordinary unescaped HTML, there are a variety of open source Java HTML parsers out there that you can use. If you are going to work with the escaped HTML as you have in your first example, you will have to write the parser yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With