I read html code from my webpage to display in my app.
How can i convert these characters into String characters in Android?
– = -
… = ...
I know those two. But I would prefer a dynamic solution if possible.
Html.fromHtml() is used primarily for converting HTML tags into spans, returning a Spanned object that retains that formatting when applied to a TextView.
However, Html.fromHtml() can also handle converting some HTML-style entities into characters.
Related
Wrote a markdown text using https://stackedit.io, copied the markdown output, and pasted it into a Firestore field to be rendered in my android app using the markwon library.
However, I noticed that the text from Firestore did not render properly on the android but the raw markdown text when used directly on the app rendered well. After investigation, discovered that Firestore stripes all the new line in the markdown text and only displays a large text without new line or line break.
What I have tried:
I have tried to replace \\n character with \n before rendering - Did not work
Manually add \n character in the firestore text - Did not work
Converted the markdown to html before storing in firestore - Worked but not sustainable and didn't give desired output
How do I fix this ssue such that the markdown text in firestore is rendered correctly on android?
Firestore does not support escape sequences in string values. It simply does not understand any sort of escape sequences that might have a special meaning in a particular programming language.
This means that if you write "\n" in a string-type field, and later when you want to read it, you'll read exactly that backslash and the n character. If you need to store special escape sequences like \n or \\n, then you consider encoding and decoding that yourself.
I'm getting a JSON response string similar to this:
<strong>B.<\/strong> Because there is no indication of Miss Manette’s feelings
The string text that I'm receiving is full of tags like <strong>, <em> and ’
“
” etc. How can I parse it to a plain String with same features?
The only way I could think of is replacing such characters and using Html.fromHtml() method. Is there a built-in parser available? How could I parse such HTML text?
Use Html.fromHtml only. It'll parse most of the tags supplied and give you the formatted output. The point to note here is that not all of the HTML tags are supported by this method. Checkout this link for more information about what tags are supported. Also check this, though it's a bit old.
If you know what text you'll be parsing, and you have tags that aren't parsed by fromHtml, your best bet would be to replace them with empty string and then use this method.
After parsing json data and displaying it in a textview, I noticed that it has some html characters like &lsquo, &mdash, ", ', &rsquo,  , &ndash, and many others. How to deal with that?
These are html entities. To transform them to readable text use Html.fromHtml(). Like this:
textView.setText(Html.fromHtml(jsonString));
In Android, I have an application that handles multiple rich format text fields. I get the description of the text from an xml and create it as an spannable string builder, adding each run and styling it.
Is there a way to store this on sqlite that doesn't imply storing the whole XML describing the paragraph?
I know it can be done in iOS but I haven't found a way for Android.
Thanks in advance for any answers or tips.
Is there a way to store this on sqlite that doesn't imply storing the whole XML describing the paragraph?
I do not know what "the whole XML describing the paragraph" is. You can:
Use Html.toHtml() to generate HTML from a Spannable, or
Roll your own code to convert a Spannable into something that can be stored as a string or byte array
I am fetching few html content from my server for which I am using JSON parsing. But this converts my html content to unicode values.
For Eg: <p>Spend minimum $10 (in a single same-day receipt) at any outlet<\/p> is getting converted to,
;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>
Now if I try to set this to my WebView it displays with HTML tags itself. If I try to encode the data using TextUtils.encode it displays the text with unicode values.
Can anyone help me with this.
How should I fetch a HTML content and display it in WebView?
I am not getting your question exactly but, If you want to load HTML in web view in you can use
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
and if you want to convert < and > like notation you can use Jsoup Library
Guys thanks for your help. But I have solved this issue myself. I have elaborated my way of solving the issue.
What I did is,
1)convert the unicode value to Spanned like this,
Spanned ss=Html.fromHtml(;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>");
2)Now convert this Spanned to String like this,
String tempString=ss.toString();
3)And now set this to WebView which solved the problem,
webView.loadData(tempString, "text/html","UTF-8");
Actually this isn't JSON encoder converts data to HTML entities but some other layer, before it passed to JSON encoder.
JSON have nothing to do with HTML tags, usually only quotes encoded by parser (Unicode is supported by most parsers).
You probably need to change the way data is returned by server, to omit encoding of HTML tags braces to HTML entities or decoding entities backin your app.
Update:
To decode HTML entities used in HTML tags (and others too) you may use StringEscapeUtils.unescapeHTML()
To show the HTML page inside the Webview why you require the JSON. create web view inside the XML and write below code Inside the Activity you can see the HTML page.
webView = (WebView)findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
webView.loadUrl("http://www.google.co.in/");