Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide the "Web page not available" page/error from a webview?

Tags:

android

I want to hide that error and only show a blank page and a dialog.

How can I hide it?

like image 886
code511788465541441 Avatar asked Dec 12 '22 21:12

code511788465541441


1 Answers

It is not so obviously. Because WebViewClient will open standart error page anyway, even if you override onReceivedError method. So we need to open custom error page after handle error event.

So, you should override onReceivedError in WebViewClient, then if you handle needed error code (see ERROR_ constants in WebViewClient) you should open blank page or another page to hide standart Android "Web page not available" page.

Something like this:

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    if (errorCode == neededErrorCode) {
        hideErrorPage(view);
    }
}

private void hideErrorPage(WebView view) {
    // Here we configurating our custom error page
    // It will be blank
    String customErrorPageHtml = "<html></html>";
    view.loadData(customErrorPageHtml, "text/html", null);
}
like image 113
Artem Zinnatullin Avatar answered Jun 01 '23 00:06

Artem Zinnatullin