Mdlc Mdlc - 1 year ago 97
Android Question

How can I show a custom "webpage not available" page in Android WebView?

I'm out to do something when no connection is available page/alert in

(e.g. load a local html page or alert). I've to play with Prevent WebView from displaying "web page not available" but without any success. Any suggestions would be appreciated.

Answer Source

It all came down to simply showing an AlertDialog from onReceivedError:

 public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                    //Clearing the WebView
                    try {
                    } catch (Exception e) {
                    try {
                    } catch (Exception e) {
                    if (webView.canGoBack()) {

                    //Showing and creating an alet dialog
                    AlertDialog alertDialog = new AlertDialog.Builder(youractivity.this).create();
                    alertDialog.setMessage("No internet connection was found!");
                    alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {


                    //Don't forget to call supper!
                    super.onReceivedError(webView, errorCode, description, failingUrl);

If you're new to WebView, you'll be looking to implement onReceivedError like this:

mWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download