Had to load data twice to make WebView refresh in Android - android

When I first create the activity, everything goes fine. However, after I choose from menu to change some text of the String values and set the webview by
webview.loadData(result, "text/html; charset=UTF-8", null);
webview.loadData(result, "text/html; charset=UTF-8", null);
I have to do it twice, or the webview will keep unchanged. Is there anyone knows what happens here? Since the result String is the same, why webview force me to loadData twice?

Avoid WebView#loadData(String data, String mimeType, String encoding) - it's buggy.
Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead.
So your instruction will be like:
webview.loadDataWithBaseURL(null,result,"text/html", "utf-8", null);

Don't know what's your problem but looking at the webview documentation, you are using the loadData method wrongly :
Webview:loadData documentation
You probably should call your webview like this :
webview.loadData(result, "text/html", "UTF-8");
Don't know if it will solve your issue at all.

Yes with loadDataWithBaseURL it does refresh the data, but then it ignores the CSS body background-color! ... At least it can't parse "%23000000" which works with loadData.

I am loading local HTML data into my webview, and this webview is inside recyclerview,
When I try webview.loadData() when it renders 1st time it working fine, but when I scrolling upward downward every inflated webview`s get messed-up.
When I try second webview.loadDataWithBaseURL() its working like charm.
so,when you're loading the HTML locally and it references assets such as images & css which are also packaged locally use webview.loadDataWithBaseURL()

Related

android webview can not show content by loaddatawithbaseurl sometime

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");

Limit in Loading content into WebView

i have a problem for load content into my Web View.
The problem is:
i have string like this:
String temp=myobj.get(0).getstring; //very long string
webview.loadDataWithBaseURL("", temp, "text/html; charset=utf-8", "utf-8", "");
and when i get the result of
myobj.get(0).getstring; //very long string
using Log, and set it manually, every thing was OK.
so i really don't know what is the problem.
do any one know what is the reason of the problem?
EDIT: in fact i test a couple of more thing, and i understand it show string but don't show images.

Android Webview goback() issue with loadDataWithBaseURL method

Issue is goback() not showing my html data back. Steps to produce this issue is like
Loaded html data using method loadDataWithBaseURL. It renders html data fine.
Then click on one link inside html data then webview moves to next page showing that link which is also fine.
When I call method goback() from this page it should show my html data but it is showing me blank screen. Inside onPageFinished() I am getting url as about:blank.
Thanks in advance!
If you use loadDataWithBaseURL you will need to send in the url parameter something different from null, if you send null the url will always be "about:blank"
Example:
var page = new RazorView().GenerateString();
webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", "");
var url = webView.CopyBackForwardList().GetItemAtIndex(1).Url;
//url will get the Html From Previous Page
Notice im using C# but it should be the same with java, except for the CapitalizedWords

How to clear html page before showing into a webview in Android?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

Hindi Character will not display in web view correctly

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.

Categories

Resources