How to hide webview error page. Is it possible?

I searched and found similar questions, but they mostly tell how to change the contents of a webView, and not how to hide it.

My webView is initially hidden using android: visibility = "gone" in main.xml, I change it dynamically to visible using myWebView.setVisibility (1); when the page is fully loaded (and it works). Now I want to hide this webView when an error is detected. The reason I wanted to hide this is because I have a nice background in the layout that reports an error. I know that this is not the best approach for this, and probably change it later, but now I would like to decide why the web browser does not hide when an error occurs (just for fun, maybe).

Here is what I tried:

@Override
public void onReceivedError (WebView view, int errorCode, 
                             String description, String failingUrl) {

        myWebView = (WebView) findViewById(R.id.webview);  
        // myWebView.setVisibility(0); // Doesn't work!

        // if (errorCode == ERROR_TIMEOUT) { // Commented just for trying

        try {view.stopLoading();} catch(Exception e){}
        try {view.clearView();} catch(Exception e){}

            view.loadUrl("file:///android_asset/error.html"); // This Works but I don't want it this way.
            view.setBackgroundColor(0x00000000); // Trying to make it transparent. Doesn't work here
            view.setVisibility(View.GONE); // Doesn't work. I have tried also with myWebView.
            //  }
    }

Any ideas?

+6
2

:

 boolean isPageError = false;

 webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
           isPageError = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (isPageError){
                webview.setVisibility(View.GONE);
                txtError.setVisibility(View.VISIBLE);
                txtError.setText("error message");
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
           isPageError = true;
        }
    });
+3

private boolean isError = false;

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.e(TAG, "onPageFinished");
        if (isError) {
            //Show error
        } else {
            //Hide error
        }
    }

    @Override
    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        super.onReceivedError(webView, errorCode, description, failingUrl);
        Log.e(TAG, "onReceivedError Old = " + errorCode);
        if (errorCode == -2) {
            isError = true;
        }
    }

    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        Log.e(TAG, "onReceivedError New = " + error.getErrorCode());
        if (error.getErrorCode() == -2) {
            isError = true;
        }
    }
}
0

All Articles