Using webView.loadData(String data, String mimeType, String encoding) on Android for displaying an html content inside a webView won't show the Chinese characters correctly. Using loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) with null values for "baseUrl" and "historyUrl" will solve the problem. Is there for such behavior?
webView.loadData: Does not display the Chinese characters correctly
webView.loadDataWithBaseURL: Displays the Chinese characters correctly`
webView.loadData("html content containing Chinese characters", "text/html", "UTF-8"); //does not work
webView.loadDataWithBaseURL(null, "html content containing Chinese characters", "text/html", "UTF-8", null); //works
Habe a look here :
Android Chinese characters in WebView
Be sure the content of the website is UTF-8 / same enconding you use, too.
I have a very simple WebView that needs to display text as HTML. I retrieve data from a server that is in a byte array. I need to display that byte array as readable text.
var wv = FindViewById<WebView>(Resource.Id.webview);
var container = AutoFac.Container;
_routinesService = container.Resolve<IRoutinesService>();
byte[] documentHtml = _routinesService.GetFragment(documentId);
string mimeType = "text/html";
string encoding = "utf-8";
var html = System.Text.Encoding.UTF8.GetString(documentHtml);
wv.LoadDataWithBaseURL("", html, mimeType, encoding, "");
I try to convert the byte array to a String, and the variable html becomes \"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg==\"
This looks like Base64 to me, and when I use an online converter (https://www.base64decode.org/), it does convert to the correct text. But the output on the screen in the WebView just looks like "U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg=="
Any ideas what I might be missing?
Just for more information, this is in Xamarin.Android so the code is C# instead of Java (but there is not much difference).
Have you tried javascript
atob()
method that decodes a string of data which has been encoded using base-64 encoding before posting it to webview.
I need to plug an HTML formatted string into a TextView. I'm retrieving the text from a database and when I insert it using this code
WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");
It works but this code
TextView output = (TextView) view.findViewById(R.id.simple_text);
output.setText(Html.fromHtml(text));
strips out all HTML formatting.
What is the right way to do this?
Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them.
Then, limit your HTML to use tags that are supported by Html.fromHtml().
Actually i am retrieving text from json web service which is HTML text containing tags and all that ,which i am executing with Html.fromHtml ,but the problem is that the Special Characters are displayed as diamond with question mark which i am not able to display in its correct form.
below is the code where i got the problem
Description=Description.replaceAll("\\<.*?>","");
Description=Description.replaceAll("\\“", "");
Description=Description.replaceAll("\\”", "");
// Description=Description.replaceAll("♦", "");
//Description=Description.replace("“", "");
details.setText(Html.fromHtml(Description).toString());
Description is the String variable where i have stored the html text,i have googled alot but doesn't get anything.hope i got something from here
thanks in advance
In my webview I did sothing like this.
productInfoWebview.loadData(value, "text/html; charset=UTF-8", null);
Decoding HTML is decoding HTML entities to Java raw unicode characters.
String html = "B & This is HTML";
String java = Html.fromHtml(html);
> Output: "B \u0026 This is HTML"
String strJava = Html.fromHtml(html).toString();
> Output: "B & This is HTML"
I'm loading some data, containing latin-1 characters, in a WebView using
String uri = Uri.encode(html);
webview.loadData(uri, "text/html", "ISO-8859-1");
When displayed, the latin1 characters are replaced by weird characters.
If I load the html directly in a TextView (just to test), latin characters are properly displayed.
Anybody can help?
Thanks
html:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- some html -->
</html>
myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
This works flawlessly, especially on Android 4.0, which apparently ignores character encoding inside HTML.
Tested on 2.3 and 4.0.3.
In fact, I have no idea about what other values besides "base64" does the last parameter take. Some Google examples put null in there.
You should always use UTF-8 encoding. Every other character encoding has become obsolete for many years already.
Only way to have it working, as commented here:
webview.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");
No URI encoding, utf-8... loadData bug?
String start = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body>";
String end = "</body></html>";
webcontent.loadData(start+ YOURCONTENT + end, "text/html; charset=UTF-8", null);
One of solution of problem.
I have display © 2011 and it was displaying ©.
With the below code i have achieved displaying correct value © 2011
webViewContent.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
AFAIK that:
Firstly, loadData() method is used to load raw html code.
Secondly, just put the html code directly to the loadData(), don't encode it
You might wanna try like this:
webview.loadData(uri, "text/html", "ISO-8859-1");
Cheers!
I too had the problem of getting a weird character like  here and there. Tried different options, but the one that worked is below.
String style_sheet_url = "http://something.com/assets/css/layout.css";
String head = "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"" + style_sheet_url + "\" /></head>";
String locdata = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + head + "<body>"+ data + "</body></html>";
wv_news_text.loadData(locdata, "text/html", "utf-8");
wv_news_text is the WebView.
Info from Java docs about loadData method
Loads the given data into this WebView using a 'data' scheme URL.
Note that JavaScript's same origin policy means that script running in
a page loaded using this method will be unable to access content
loaded using any scheme other than 'data', including 'http(s)'. To
avoid this restriction, use loadDataWithBaseURL() with an appropriate
base URL.
The encoding parameter specifies whether the data is base64 or URL
encoded. If the data is base64 encoded, the value of the encoding
parameter must be 'base64'. For all other values of the parameter,
including null, it is assumed that the data uses ASCII encoding for
octets inside the range of safe URL characters and use the standard
%xx hex encoding of URLs for octets outside that range. For example,
'#', '%', '\', '?' should be replaced by %23, %25, %27, %3f
respectively.
The 'data' scheme URL formed by this method uses the default US-ASCII
charset. If you need need to set a different charset, you should form
a 'data' scheme URL which explicitly specifies a charset parameter in
the mediatype portion of the URL and call loadUrl(String) instead.
Note that the charset obtained from the mediatype portion of a data
URL always overrides that specified in the HTML or XML document
itself.
Following code worked for me.
String base64EncodedString = null;
try {
base64EncodedString = android.util.Base64.encodeToString((preString+mailContent.getBody()+postString).getBytes("UTF-8"), android.util.Base64.DEFAULT);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(base64EncodedString != null)
{
wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");
}
else
{
wvMailContent.loadData(preString+mailContent.getBody()+postString, "text/html; charset=utf-8", "utf-8");