How to Know That My Webview Is Loaded 100%

How can I know that my WebView is loaded 100%?

As said here: How to listen for a WebView finishing loading a URL?

boolean loadingFinished = true;
boolean redirect = false;

mWebView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}

loadingFinished = false;
view.loadUrl(urlNewString);
return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}

@Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}

if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}

}
});

How to listen for a WebView finishing loading a URL?

Extend WebViewClient and call onPageFinished() as follows:

mWebView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

Anyway To tell when content in a webView is fully loaded?

You can call WebView.getProgress() and see if it's at 100%.

EDIT: Add this to your WebView:

WebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

As mentioned in this question.

Android Webview: Detect when rendering is finished

Finally I got the very detailed for this problem. This explains why it always returns null bitmap.

Every change in my app are triggered by a user action so I came up with a modified version that only trigger invalidate() after a touchEvent

Thank for all your help.

Android WebView renders blank/white, view doesn't update on css changes or HTML changes, animations are choppy

How to check url is loaded in webview or not

public static boolean isOnline(Context context) {

try {
ConnectivityManager cm = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {

URL url = new URL("http://www.google.com.pk/");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setConnectTimeout(1000); // mTimeout is in seconds

urlc.connect();

if (urlc.getResponseCode() == 200) {
return true;
} else {
return false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

WebView.getProgress() always returning 100

You need to implement a WebChromeClient to capture the progress change with the onProgressChanged() method:

http://developer.android.com/reference/android/webkit/WebChromeClient.html#onProgressChanged(android.webkit.WebView, int)

See the answer to this question: Android WebView progress bar

And you can actually see an example in the WebView documentation: http://developer.android.com/reference/android/webkit/WebView.html

Android reliable method for getting the height of a webview

I think I finally found a solution after trying quite a few things that didn't work. It seems that the problem with using the onPreDraw function and waiting for the content height to not equal zero is that the content height may equal some intermediate value if the webview is loading an image. I also tried the WebViewClient method onPageFinished but that would also fire even though the page was still loading images. I also attempted to override the invalidate method of the WebView which I didn't understand but that also had the same issues. The solution that worked for me was to actually call a javascript function. Javascript seems to have more methods available for detecting a page load that also includes when all the content, including the images have downloaded. I did the following:

I added a javascript function into the html. This may not be an option for some people but I have complete control over the html content.

<script>
function loadAlert() {
var loadFlag;
if (document.readyState === "complete") {
loadFlag=1;

}else{
loadFlag=0;
}

window.pageload.sendToAndroid(loadFlag);
}
</script>

I'm not familiar with javascript but basically this function sets the variable loadFlag to 0 if the page has not loaded and 1 when it is loaded. The document.readyState is equal to complete when all the content has loaded which includes images. This function is then queried from Android.

The following class was added to the android project. This was just appended to the end of the main fragment class in the file I was working with. This snippet of code was modified from a previous Stackoverflow post. I don't quite understand this but it sets up some sort of interface for the javascript.

    final class IJavascriptHandler {
IJavascriptHandler() {
}
// This annotation is required in Jelly Bean and later:
@JavascriptInterface
public void sendToAndroid(int loadFlag) {
// this is called from JS with passed value
jsLoadFlag=loadFlag;

}
}

The following code was placed inside the onCreateView (this is a fragment). After getting the webview, javascript is enabled. The last line sets up the interface which was defined above.

    webview = (WebView)v.findViewById(R.id.webView2);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new IJavascriptHandler(), "pageload");

The rest of the code sets up a PreDraw listener. From the documentation, this is called when the view tree is about to be drawn but that doesn't mean all the content is loaded. This is why some posts have suggested checking for when webview.getContentHeight is not equal to zero. The problem I had is that I would get a non-zero number while pictures were loading.

The first if statement checks to makes sure the getContentHeight is not equal to zero and that the webview progress is 100 so the webview thinks that it has loaded. I put the javascript call inside this if statement so that the javascript calls are minimized. This also allows for some delay on the javascript call when the screen is rotated. I'm not sure when exactly it would get set back to zero on a screen rotation but this adds some buffer.

jsLoadFlag is a variable that gets set in the javascript interface class. It's visible through the entire file. It's important to set it to 0 in the onCreateView method otherwise on a rotation is may retain it's previous value of 1. This variable should only be 1 if the page has finished loading. The code that I want to run only runs when webview.getContentHeight!=0 and webview.getProgress==100 and jsLoadFlag==1. If those conditions are met then the page has loaded. Inside the IF statement, I then scroll the webview to the proper position.

    //Reset jsLoadFlag to account for rotations.
jsLoadFlag=0;

viewTreeObserver = webview.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {

if ( webview.getContentHeight()!= 0 && webview.getProgress()==100){

//This function sets the jsLoadFlag variable
webview.loadUrl("javascript:loadAlert();void(0)");

if(jsLoadFlag==1){

//Entire page including all pictures should have loaded so getContentHeight should be accurate
int newPosition=(int)Math.round((webview.getContentHeight()*scrollPercent));
webview.scrollTo(0, newPosition);

webview.getViewTreeObserver().removeOnPreDrawListener(this);
}
}
return false;
}
});

onProgressChanged doesnot hit 100% on heavy sites

Nevermind. Chromium accpected my bug.

https://bugs.chromium.org/p/chromium/issues/detail?id=650781



Related Topics



Leave a reply



Submit