views:

57

answers:

1

Hi, I have an issue with Webview. I'm programming my own WebChromeClient class overriding onProgressChanged method because I want to show the page when it finishes loading and then, to hide the splash screen. As I just want it to happen with a specific url, I compare the actual WebView url with a specific string, but there is a problem, I get a null pointer when webview.getUrl() method is called and my application finishes. This is the code:

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged (WebView webview, int newProgress) {
        super.onProgressChanged(webview,newProgress);
        if(webview.equals(w1) && newProgress == 100 && webview.getUrl().startsWith("https://ssl.facebook.com/login.php")) { 
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

I do this for avoid the webview takes three or four seconds to render the page, but it doesn't work. The code I used before was:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        super.shouldOverrideUrlLoading(webview, url);
        webview.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView webview, String url) {
        if(url.startsWith("https://ssl.facebook.com/login.php")) {
            webview.setVisibility(WebView.VISIBLE);
            ImageView imageview = (ImageView)findViewById(R.id.ivsplash);
            imageview.setVisibility(ImageView.GONE);
            ProgressBar progressbar = (ProgressBar)findViewById(R.id.pbsplash);
            progressbar.setVisibility(ProgressBar.GONE);
        }
    }
}

Thanks

+1  A: 

Not sure if this is possible with WebChromeClient, but I was able to do this using

WebViewClient and overloading onLoadResource, onReceivedError and onPageFinished

This is what I've done

onLoadResource     
 mwebview.setVisibility(View.GONE);


onReceivedError
mwebview.setVisibility(View.VISIBLE);

onPageFinished
mwebview.setVisibility(View.VISIBLE);

and intially in XML the webview is set as GONE

android:visibility="gone"

I too use it for Facebook and works fine.

Pentium10
Thanks for the reply.I've tried that, but when I hide the splash screen, the webview stay blank three or four seconds until the page is rendered. That is I want to fix and I don't know how to avoid that.I've edited my question with the code I used.
Adrian
I updated my draft code. Please note that some url override methods are also called for other elements like images, css.
Pentium10
The solution you gave me is good although it exists some flickering because there are some page redirections and the onPageFinished is called two or three times, so the webview is shown but OnLoadResource hide it again until the last page is loaded. As I said, the solution works very fine but I would like to fix that issue.Thanks for all.
Adrian
I know about the redirections and flickering, if you track them down you can optimize it more.
Pentium10