How to hide the WebView error page. Is it possible?

6

I have searched and found similar questions, but they mostly say how to change the webView content, not how to really hide it.

My webView is initially hidden using android:visibility="gone" in main.xml, I change it dinamically to visible with myWebView.setVisibility(1); when the page is fully loaded (and it works). Now, I want to hide this webView when an error is detected. Reason why I wanted to hide it is because I have a nice background in the layout that informs about the error. I know this is not the best approach to do this, and probably change it later, but now, what I would like to resolve is why the webView is not hidding when an error happens (just for fun, maybe).

This is what I've 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?

android
error-handling
webview
asked on Stack Overflow Jan 25, 2013 by Jorge • edited Aug 30, 2015 by QED

2 Answers

3

This is my idea:

 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;
        }
    });
answered on Stack Overflow Apr 7, 2017 by K. Sopheak • edited Nov 12, 2017 by K. Sopheak
0

It's worked for me

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;
        }
    }
}
answered on Stack Overflow Jan 17, 2019 by Renish Patel

User contributions licensed under CC BY-SA 3.0