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.
Related
I use webview to load local html data like this
webview.loadDataWithBaseURL("about:blank", finalSrc, "text/html", "UTF-8", null);
the finalSrc is a variable of html string.
sometimes, the webview can display the corrent content,but sometimes not.
and I found that if I clear the cache of my app, the webview works well again.
so what's wrong with my app?
rather than using BaseUrl why don't use loadData. for example
webview.loadData(finalSrc, "text/html", "UTF-8");
I am using the following code for rendering a dynamically created html string on a webview. It works properly on my device. But on one of the user's device, the webview displays the raw html tags and content instead of rendering properly.
I cannot reproduce the issue on my devices. What can be cause for this issue to happen. Any device level settings? or Encoding related issue?
String htmlContent = "<html><body> .... </body></html>";
webview.loadDataWithBaseURL(null, htmlContent, "text/HTML", "UTF-8", null);
Try this code. It works for me.
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);
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.
Can any one please tell which other characters need to be converted before loading the data in webview.
view.loadDataWithBaseURL("data:",htmlString, "text/html",
"utf-8", htmlString);
Try ("data://", htmlString, "text/html", "utf-8", null).
That should load, although the back button behavior might not be correct.