I have a text (sInfoText) with a lots of special characters like 'è'.
I would like to show this special character also in a webview, but I get rubbish characters. Howto convert this special characters programmatically?
wvinfo.loadData(sInfoText,"text/html", "UTF-8");
try
wvinfo.loadData(sInfoText, "text/html; charset=UTF-8", null);
this ll work on above 4.0.0
Related
One of our application's users says that he cannot read news comment because of wrong symbols charset. He has Galaxy Ace Duos (GT-S6802) and made a screenshot:
Nobody else complains about the error. What could be the problem?
The app loads comments (UTF-8) from server API. Text of each comment has html format (contains images) and therefore is placed in a WebView this way:
holder.text.loadData(text, "text/html; charset=UTF-8", "utf-8");
App on Play Market: https://play.google.com/store/apps/details?id=kz.sportlive
I solved my problem. But before it I tried to encode comment text to Base64 on server and decode it on app - didn't work.
Then I tried load encoded text in WebView:
webview.loadData(comment.text_base64, "text/html; charset=UTF-8", "base64");
It didn't work too.
Finally I tried this way:
webview.getSettings().setDefaultTextEncodingName("utf-8");
webview.loadData(comment.text, "text/html; charset=utf-8", null);
and it works perfect!
Use it in similar situations
Some of the not-show characters:
ḍ - ḍ
ḥ - ḥ
ḫ - ḫ
ḳ - ḳ
All the characters that not shown in Android WebView, HTML Entity's are greater than 7000.
I tried the solution below but it didn't work.
WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
myWebView.loadDataWithBaseURL(null, "#ḍḥḳ#", "text/html", "utf-8", null);
It showed in WebView : " "
I'll be very thankful for any advice.
Using entities for such symbols isn't best idea at all.
You should use UTF-8, which can hold virtually any character. For solution, you've showed (that doesn't seem to work) -- make yourself sure, that either document content or string, you're passing is actually encoded in UTF-8. It's not just setting the encoding, but actually encoding correctly the file as well.
And don't mix up entities with UTF-8. Either use one or another solution to encode special characters.
I had Database in which data stored in hindi as \u092e\u0948\u0902 \u0924\ and setting that content to webview using below.
webview1.loadData(hindi_content, "text/html", "UTF-8");
But it will display as
I don't know why that's happening. Any one please suggest. how to fix that !
This happens because of a bug with the encoding parameter of loadData in most Android versions. This parameter is ignored for some reason so the UTF-8 based hindi characters will not be rendered.
To fix this you can use one of the following alternatives.
webview1.loadData(hindi_content, "text/html; charset=UTF-8", null);
webview1.loadDataWithBaseURL(null, hindi_content, "text/html", "utf-8", null);
This is a duplicate of this answer:
You will also need to unescape those sequences and to do that refer to How to Unescape Unicode in Java
Rendering UTF-8 in a WebView using loadData has been broken in some form or fashion forever.
Issue 1733
Use loadDataWithBaseURL instead of loadData.
// Pretend this is an html document with those three characters
String scandinavianCharacters = "øæå";
// Won't render correctly
webView.loadData(scandinavianCharacters, "text/html", "UTF-8");
// Will render correctly
webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null);
Now the part that is truly annoying is that on the Samsung Galaxy S II (4.0.3) loadData() works just fine, but testing on the Galaxy Nexus (4.0.2) the multi-byte characters are garbled unless you use loadDataWithBaseURL(). WebView Documentation
you will need to use font in order to support hindi (Hindi language is not yet fully supported by android)
create Singleton instance of Typeface and invoke createFromAsset();
and add it to WebSettings like this
WebSettings webSettings = webView.getSettings();
webSettings.setFixedFontFamily(InstaceOFTypeFace);
Finally I have come up with the solution of Loading hindi content to the webview.
I had simply change my loading string and unfortunately it will work.
webview.loadData(Hindi_css, "text/html; charset=UTF-8", null);
Thank you all for your effort. :)
You can use this one also.
String uri= Uri.encode(html file/url);
webView.loadUrl(uri);
may be this will help you.
I am going to get the text in an EditText and then display the text in a WebView. The following code works for ASCII characters. For non-ASCII characters, the text in WebView becomes garbage characters.
String input = mEditText.getText().toString();
String html = makeHTML(input); // append HTML elements and headers including MIME and ENCODING header
mWebView.loadData(html, "text/html", "utf-8");
I thought that I was doing something wrong with my HTML, so I try to display the text directly in the WebView without modify the text. However, the result was the same.
String input = mEditText.getText().toString();
mWebView.loadData(input, "text/html", "utf-8");
The makeText() of Toast which displays non-ASCII text in EditText without any problem.
Does anyone know the answer?
WebView might not be able to load certain "unsafe" HTML characters. Try using:
String input = mEditText.getText().toString();
String html = makeHTML(input);
String encodedHtml = URLEncoder.encode(html,"UTF-8");
mWebView.loadData(encodedHtml, "text/html", "utf-8");
The URLEncoder.decode(encodedHtml,"UTF-8") method might also be useful.
Finally, I solve the problem by using loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl) of WebView
mWebView.loadData(data, mimeType, encoding)
mWebView.loadDataWithBaseURL("", data, mimeType, encoding, "")
seems to be same but actually does not.
In my case, loadData() failed to encode the characters properly and failed to load images saved in the asset folder.
If I push this HTML into WebView:
webView.loadData("<html><body><pre>line 1\nline 2</pre></body></html>", "text/html", "utf-8");
it renders as (in emulator and also on device)
line 1line 2
as opposed to
line 1
line 2
as I would expect. If I save this HTML to the sdcard and open the file in the browser, it renders fine. I suppose I am doing something wrong, or this may be a bug. Any way, I want to programatically push HTML with preformatted newlines into a WebView and have the newlines rendered.
The string passed to loadData needs to be URI-escaped.
You can use URLEncoder.encode() to do that, but for some reason WebView does not decode the '+' back to a ' '. One work around is to replace all the '+' with '%20' yourself.
For example (and with the '+' translation):
try {
webview.loadData(URLEncoder.encode("<html><body><pre>line 1\nline 2</pre></body></html>", "utf-8").replaceAll("\\+", "%20"), "text/html", "utf-8");
} catch (UnsupportedEncodingException uee) {
Log.e("webview", "", uee);
}
Try this:
webView.loadDataWithBaseURL(...)
More info here
Also you can use
chapterWebView.loadDataWithBaseURL("file:///android_asset/NTImages/", message.replaceAll("\\n", "<br/>") , "text/html", "utf-8", "utf-8");