WebView does not display loaded Html on some phones - android

I have a WebView in one of my Activities where I want to load a Html page. The page contains jquery-mobile and some html. So I do the following in my Activity :
mWebView=(WebView) findViewById(R.id.MyWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
[...]
});
mWebView.loadUrl("http://www.mymobilepage.html");
The problem is that the page gets loaded and displayed on the emulator, and on a HTC Desire, but when I try to load it on a LG Optimus One nothing gets displayed. The events onPageStarted and onPageFinished both get fired in my WebViewClient but just a blank page is displayed, and also I don't have any errors in my LogCat.
Thanks in advance.

When onPageFinished is called, the page may not be completely rendered. The documentation states:
Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
However, note that onNewPicture is documented as deprecated and obsolete. I ask about a replacement/alternative here.

This should be a comment, but since there is a bit of code on it I've added as response.
Try changing default background to transparent and alerting as soon as the page is loaded, just to be sure that at least the html is being interpreted:
mWebView = (WebView) this.findViewById(R.id.webview);
mWebView.setBackgroundColor(0);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { alert('hello'); })()");
} });
and when loading the webpage:
mWebView.clearView();
mWebView.loadUrl("http://yourmobilepage.something/");
and let us know if something happened.

Try this:
webView.post(new Runnable() {
#Override
public void run() {
// Your code here...
}
});

Have you checked your html/js code with different versions on the emulator? Newer Android versions have newer versions of WebKit, that might be the problem.
I would also check if you have LogCat set to show Error messages only, or Debug+Info+Warning+Error messages. According to this, the javascript errors should show up as Debug messages.

I had a similar issue to this, I found that calling clearview and then reload seemed to clear it up -- as in:
mWebView.clearView();
mWebView.loadUrl("http://yourmobilepage.something/");
mWebView.reload();

Related

Android Webview onReceivedTitle not called on page reload

I've been trying to inject some js into a webview in my app. Have been using
onReceivedTitle method of WebChromeClient since I want to execute the js while the page is loading.
This has been working until now. But recently, I observed that onReceivedTitle is not called we reload the webpage, similar to window.location.reload.
Firstly, I can't understand why it shouldn't be called. Or it should be and it's a bug?
Secondly, now that we know it's not called, where else can I inject by js?
Thanks.
Apparently, this is not considered an Android bug, but rather a Chrome or Chromium-based WebView bug. It should be reported to http://crbug.com/.
I'll suggest you to try a workaround with custom WebViewClient:
You'll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.
Below is a sample implementation of the above:
WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
ExperimentingActivity.this.setTitle(view.getTitle());
}
});
This answer here will prove to be useful:https://stackoverflow.com/a/8193479/9080948
Seems like this is a bug in chromium. I worked around this by injecting my scripts on onPageFinished method of WebviewClient.

Android : Is it possible to show jquery or javascript popup?

is it possible to show jquery or javascript popup message in android WebView ?
You can display popup message with the JavaScript alert(), confirm(), or prompt(). If they are not working then something else is wrong. Ensure your JavaScript is properly reaching those statements. Use console.log() to write messages to LogCat to trace your execution. Also make sure JavaScript is enabled. IE:
myWebView.getSettings().setJavaScriptEnabled(true);
You can simply use the line given below to show a alert in webview:
wv.loadUrl("javascript:alert('hello Android');");
If you want to make some changes in UI you can call javascript like given below in overridden function onPageFinished()
wv.setWebViewClient(new MyWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wv.loadUrl("javascript:document.getElementById('client').style.visibility ='hidden';");
}
}

How to setVisibility(View.VISIBLE) to a webView AFTER its loaded its contents?

In my app there is an advertising web view, that is calling an url when the activity starts.
Everything is nice except one minor thing, it is more like a visibility issue...
So the thing is when i start the activity i literally see the page loading.. About in 0,3 seconds the texts appear then the pictures, and all the content somehow floating in from left to right. It not so nice, i would like to set the visibility of my webView to VIEW.GONE until it is done with the loading, and after that i could set it VISIBLE...
Is there a good, and working way for achieve this?
It is important that it is must work on Android 2.3 to 4.0 in each and every OS version.
I already tried onPageFinished and PictureListener related to this stack question here, but no hope, onPageFinished only works properly on 4.0 and PictureListener is for pictures which is not the best way to do this in case of different advertisings with different number of pictures. (and what if there is no picture at all...)
Please if you know a way let know with me. Thanks all.
have you tried WebChromeClient ?
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
// dismisses the PD
dismissLoadingDialog();
}
}
});
webView.loadUrl("your-Url");
// show message dialog here
createLoadingDialog();
i have used this on 2.3 to 4.0 OSes without any issue
Use webview visibility during onPageFinished method
view not display util all page loading.
webView1.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView1.setVisibility(View.VISIBLE);
}

How do I detect when a WebView page is trying to close?

I have a WebView into which I'm loading the facebook sharer php page. This page doesn't have any form of confirmation, it simply closes the window once the user has either shared or cancelled.
Since it is loaded into a web view there is nothing to "close" perse, so, I need to detect the window trying to close and act on that callback.
From my various searches it appears that the way to handle this is via a WebChromeClient. So, I'm attempting the following, but the callback is never called. I am obviously missing something, but don't know what. It *feels like the onCloseWindow event should be Overriden, but that causes eclipse to complain (reasonably) that I need to super the instance, which the documentation doesn't seem to suggest is possible.
All help appreciated.
[EDIT] oh, and I just tried swapping the order of the actions, setting the WebChromeClient before calling the url, and (as I expected) that didn't alter the behavior in any way.
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.facebook.com/sharer/sharer.php?u=http://EXTRACTED.com?img_id=abc");// + getIntent().getStringExtra("userId"));
WebChromeClient wbc = new WebChromeClient(){
public void onCloseWindow(Window w){
Log.d(TAG, "Window trying to close");
}
};
webview.setWebChromeClient(wbc);
Whats wrong with:
public void onCloseWindow(WebView w){
super.onCloseWindow(w);
Log.d(TAG, "Window trying to close");
}

onPageFinished in WebViewClient doesn't seem to fire correctly

I have seen one other SO question on this and it was not resolved.
I am using LoadData() with application generated html.
I noticed when I add a few blank lines after new text the page scrolls and the text takes a moment to magically appear (no scrolling, it just appears as in a fade-in.).
Watching .getContentHeight() it does not update in the onPageFinshed() event, but it is updated on subsequent additions of text on the page.
I created a timer event to watch getContentHeight. Even after I found getContentHeight had changed and issued a browser.pageDown(true) the page still did on scroll to the bottom.
It seems to be working fine when entering the activity for the first time. My problem only happens with I append a few lines to newInnerHTML.
Additionally the view involved has a header and footer with the browser in the middle. The webView is NOT behind the footer.
Is this a bug, or am I missing something?
TIA,
Jim
this code is within the onCreate for the activity.
final WebView browser = (WebView) findViewById(R.id.webview);
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(browser, url);
// browser.computeScroll(); //didn't help
Log.d(TAG,"page getContentHeight: " + browser.getContentHeight());
Log.d(TAG,"page finished");
browser.pageDown(true);
}
});
This method is after the onCreate for the activity
private void UpdateMessageDisplay() {
//loadData fires onPageFinished but without the new data
browser.loadData("<html><body>" + newInnerHTML + "</body></html>", "text/html", "utf-8");
//reload() again fires onPageFinished but this time with new data
browser.reload();
}
I received a partial answer here, which does get the pageDown(true) to fire.
webview loaddata scroll setting not updating till clicked
There are still some issues.

Categories

Resources