I want to display a progress bar while the WebView is loading. I am hiding it on OnPageFinished(), but this is too early. The Webview is still rendering the image.
The WebView documentation states: "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, OnNewPicture and the PictureListener interface is deprecated and obsolete. Is there another way to know that the rendering is complete?
Unfortunately, I have found that there is no event or mechanism to truly know when the page is completely loaded and finished rendering.
How about creating a custom WebChromeClient and overriding its onReceivedIcon() or onProgressChanged() methods. The onReceivedIcon() will be triggered once the favIcon is loaded and and using the onProgressChanged(), you can enquire the progress of the webview loading. I hope this helps
Please refer to this code snippet
webView.setWebChromeClient(new CustomWebChromeClient());
You can use onProgressChanged or onReceivedIcon whatever that suits your needs.
public class CustomWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress==100) {
progressBar.setVisibility(View.GONE);
progressBar.setProgress(newProgress);
}
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
// icon received here
super.onReceivedIcon(view, icon);
}
}
I think you can add and execute JavaScript function at the end of body tag of your web page to call your JavaScript interface function to hide the progress bar. But, beware of enabling JavaScript on your WebView.
I'm suffering same problem, but there is no way to know the timing.
The only way I use is to give enough room for the rendering completion by postDelayed().
In my case 200ms is enough if u don't use heavy contents more than 200K in text.
Related
I'm writing an SDK for android which needs to injects JavaScript code into webviews it finds by enumerating the UI.
After finding a webview, the code sets a WebViewClient by using setWebViewClient() and waits for onPageFinished() to perform the actual injection.
All was working well, until I tried to use this method on a Cordova 8 application.
The WebView used by Cordova 8 is a subclass (org.apache.cordova.engine.SystemWebView) that overrides the setWebViewClient() method in the following way:
public void setWebViewClient(WebViewClient client) {
this.viewClient = (SystemWebViewClient)client;
super.setWebViewClient(client);
}
Because of the casting to SystemWebViewClient, I get a cast exception.
I want my solution to be as generic as possible, so extending SystemWebViewClient is not an option, but I still need to know when the webview page is loaded in order to inject the JavaScript.
Is there any way to get a notification when a webview has finished loading a page without using WebViewClient and webView.setWebViewClient() ?
A potential workaround would be to use WebChromeClient instead, and override its onProgressChanged method as follows:
WebView webView = new WebView(this);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
// Do your injection here
}
}
});
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);
}
In my application I need to show a progress dialog when downloading and loading content to WebView. I know about the onPageStarted and onPageFinished methods, it works, however, there is a few seconds of delay between the onPageFinished call and the time the content is actually visible on the screen.
Is there a way to dismiss the progress dialog when the content is really visible, not only loaded?
You can do this by adding a PictureListener to your webview. Note that this is deprecated, but as far as I know there is no actual replacement provided by Android at this time.
From API level 23 you can use onPageCommitVisible.
#Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
// There you go.
}
Unfortunately I have not found any way to use it on lower API levels.
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();
I'm trying to take a screenshot of content of my WebView. I creare a bitmap and then create canvas from this bitmap, then I call method drawPage(canvas) inside onPageFinished() (that is a callback from WebChromeClient), but inside onPageFinished() webView still hasn't loaded content, so I see just black page at first, and then I see previous pages(because when I call it second time previous pages are loaded to webView). How could I deal with this problem? I can solve it using hadlers with some delay, but I think there should be better way to do this.
Thanks for help.
I don't think I understand the problem, but are you asking how to trigger an event on a WebView loading a page? Could you create a WebView client and override the onPageCompleted()?
private WebViewClient viewClient {
#Override
public boolean onPageFinished(WebView view, String url) {
//here you can run your Bitmap creating code
return true;
}
}
YourWebView.setWebViewClient(viewClient);
Is this what your looking for?