Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 293
Jorge Avatar asked Jan 25 '13 10:01

Jorge


1 Answers

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;
        }
    });
like image 51
K.Sopheak Avatar answered Nov 06 '22 02:11

K.Sopheak