Search code examples
androidwebviewgeturl

WebView.getUrl() returns null


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

Solution

  • 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.