In my app I display some Html content in webview:
String webViewConent = 'this is some <span style="color:#2ecc71">sample</span> string'
webView.loadData(omowienie, "text/html; charset=utf-8", "UTF-8");
However after last app update, which was related with some other thing, for some users webview doesn't work correctly. They see only string which is before span tag. The problem is not related to any specific android version.
Same problem here, I found base64 encoding as a quick fix:
String base64 = android.util.Base64.encodeToString(html.getBytes("UTF-8"), android.util.Base64.DEFAULT);
mWebView.loadData(base64, "text/html; charset=utf-8", "base64");
I had the same issue today. As a simple workaround I replaced hex colors in CSS by RGB equivalents.
Before
<span style="color: #3050c0">Some text</span>
After
<span style="color: rgb(48, 80, 192)">Some text</span>
I found the approach in this answer on WebView encoding seems to fix this issue. Instead of using loadData, try loadDataWithBaseURL:
webview.loadDataWithBaseURL(null, html, "text/html; charset=utf-8", "utf8", null)
I have same problem. My app does not display text after span tag. Some devices works fine but some devices does not work. Upvoted!
<p><span style="color:#0000CD"><strong>Materials:</strong></span></p>
As a result of my research and experimentation, I have disabled the Android System Webview application on my device. As a result of this process, my application worked correctly. However, to me, this cannot be a solution. What are your ideas?
I think the problem is the new update in Android Webview System. The old version works for me.
My alternative solution here is to uninstall Android Webview System in your phone so that you can have the default version(this solution is not ideal if your app is uploaded in Google Playstore and have a lot of users)
The second option is you can delete your color and background color codes in html for the meantime and it will work(I think it is Android Webview System's bug and we just need to wait until they fix their issues)
I have same problem. It may be the Android WebView System's bug but the solution is using font tag to color the texts.
String webViewConent = "this is some <font style="color:green">sample</font> string";
Note : This code is not working for Hex format colors
I din't find a fix but I was able to do a work around changing the string with the original HTML. This might help someone else, AS LONG AS YOU ALREADY KNOW THE COLOURS YOUR ARE GOING TO DISPLAY:
String newString = originalString.replaceAll("color:#888888", "color:gray");
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
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've uploaded my app on the playstore and for me and the most majority of users everything works fine.
Some users,however, complained about latin accempted letters like àèìòù saying strange symbols appear in their place. On my phone these letters are successfully displayed and so on my friends' phones...I can't understand where is the problem!
I load my data like this:
view.loadData("<meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=\"UTF-8\" />"+
myText, "text/html", "UTF-8");
Solved thanks to #Sabre's link:
try adding <html></html> tags. Described here.
I am using the following code to render my webview in android -
webview.loadDataWithBaseURL(null, "Subject: "+ getSubject() +" Content: "+ getContent() , "text/html" , "UTF-8", "");
The subject and content that I receive from the server are UTF encoded and show wrongly as Ã¥,ä,ö in the log and on screen. However in iOS webview they show up correctly as å,ä,ö. How do I get them to show as å,ä,ö in android as well?
make sure the content you recieve, in tag <head> use like this: <meta charset="UTF-8" >
Sorry for my English. :)
I think it's more of a font problem than code itself. Try putting DejaVuSans.ttf font instead of DroidSansFallback.ttf on the android itself. It should fix it. I'd search forum.xda-developers.com for a solution.
It was due to how I was retrieving the message from the server. I was reading the Http response character by character so it broke up the encoding. When I started reading line by line it worked fine!
I am working on an app which needs to display some dynamically queried
HTML content, including CSS.
WebView seems to be the best implementation for this type of work.
I ran into an error when testing it out, and tracked it down to the
following css tag:
hr{width:100%!important}
Android WebView seems to be incapable of displaying any html that
includes this line.
Some research shows that the attribute was deprecated
(link: http://www.w3schools.com/tags/att_hr_width.asp), but it works
on all browsers.
Below is some html, including this line. It will render fine in any
browser.
<html>
<head>
<style type=\"text/css\">
hr{width:100%!important}
</style>
</head>
<body>
Some text
</body>
</html>
And, in Android:
String exampleCSS = "<html><head><style type=\"text/css\">" +
"hr{width:100%!important}" +
"</style></head><body>" +
"Some text" +
"</body></html>";
WebView webView = (WebView) findViewById(R.id.web_html_about);
webView.loadData(exampleCSS, "text/html", "utf-8");
The result is a "Web page not available" error in the webview.
Is this a known issue due to deprecation? Is it a bug with WebView?
Is there any known work around for such issues?
It looks like the android webview passes content using the data:// scheme, and as a result, you would have to urlencode the content for it to work as expected.
In my case, the css/html is provided to me, so I had to use the following work around:
Example
((WebView) findViewById(R.id.web_body)).loadDataWithBaseURL(null, content, null, "utf-8", null);
This seems to work, and get passed this particular issue above.
Thanks to all that answered.
Dale.
Hr is not depreciated, though it has been given symantic meaning. It indicates a change in content.
I'm not sure what is causing your issue, but I can tell you your markup as written is unnecessary. Hr is a block level element, and so it already has a 100% width.