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);
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'm retrieving Accelerated Mobile Pages (APM) formatted HTML form a webcall. I'm then trying to display that AMP in a WebView. So I'm setting up my WebView like this:
webView = (WebView) findViewById(R.id.web_view);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String content = article.getContentAmp();
webView.loadData(content, "text/html; charset=utf-8", null);
It displays content without error, but it's not displaying correctly. It ignores for example amp-youtube elements and embeded tweets.
Is there something else I have to do with the WebView or the WebChromeClient to display AMP correctly?
EDIT:
The answer below works. But in my specific case, I was using the WebView inside a NestedScrollView. Which worked fine in Android 5.0+, but below that, the amp- elements would not load.
Taking the WebView out of the NestedScrollView solved the problem. It's parent is now a relative layout.
So if you're having the same problem, try simplifying your layout so that the WebView is not deeply nested. And definitely don't put your WebView inside a NestedScrollView.
You cannot display elements such as embedded youtube videos, images, tweets etc without giving the WebView a base url. The solution was to use webview.loadDataWithBaseUrl instead on webView.loadData. You must pass it a valid url.
webView = (WebView) findViewById(R.id.web_view);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String content = article.getContentAmp();
String baseUrl = extractUrlFromContent(content);
webView.loadDataWithBaseURL(baseUrl, content, "text/html", "utf-8", "");
I have created a collapsible section in fiddler using jquery mobile, which work perfectly fine and all the section does collapse
https://jsfiddle.net/ashishrawat/cduhkLm0/
However when i take the same page and display it in webview, it doesn't work. I have inspected it in chrome and I get the same element(html) as jsfiddle, excluding the header, also there is no error in console.
My page rendering code in android is pretty standard
webView.addJavascriptInterface(
inspectView.new SurveyJSInterface(), "HTMLOUT");
//webView.loadUrl(mSurveyUrl);
//webView.loadDataWithBaseURL("file:///android_asset/", fileContent, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("file:///android_asset/index.html", fileContent, "text/html", "UTF-8", null);
webView.setInitialScale(1);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
HEader section
$(document).bind('mobileinit',function(){
$.mobile.pushStateEnabled = false;
});
Only difference which i could see is fiddler is using jquery 1.4.4 while i am locally using 1.4.5 However i don't think that matters.
Please suggest !
The reason was simple. The page is modified after dom is loaded hence need to refesh the form using.
$("#formid").trigger("create");
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.