Android WebView Webpage not available

Android webview webpage not available – Here, I will show you how to fix ‘webview webpage not available’ page instead of ‘no internet connection’ alert box page. Most of students are programmers are still struggle to fix this error. First, am also struggle to fix this problem but i have fix this error in just 1 hour.

It’s very common error but very easy to clear within 10 minutes. Initially I spend nearly 2 hours for sort out the issues, because in that time I am beginner in Java. Therefore not able fix the issues in short period of time. But now I have overall some experience to handle any type of errors, even database end.

You can change the error message box as per your own designs. But here I follow some of protocols that’s why clearly explained the exact file of code. Changes are comes from on your XML file to customize the button, text and image layouts.

Android webview webpage not available

A lot of tutorials are available on internet to fix this problem. But most of developers are waste your time and not explain the proper ways. So here just follow my below steps to clear your queries. Actually this is very simple. You have easily make this problem instead of webpage not available in no internet connection alert box. In previews post i have explain WebView Example.

Android WebView Webpage not available

Here, i have include the full code of make internet connection alert box instead of webpage not available page. The actual code is,

webView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                try {
                    webView.stopLoading();
                } catch (Exception e) {
                }

                if (webView.canGoBack()) {
                    webView.goBack();
                }

                webView.loadUrl("about:blank");
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Error");
                alertDialog.setMessage("Check your internet connection and try again.");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        startActivity(getIntent());
                    }
                });

                alertDialog.show();
                super.onReceivedError(webView, errorCode, description, failingUrl);
            }
        });

Completed code

Finally the complete code look like. Apart from this read Android Form Validation  it’s helpful you to validate forms.

package com.example.vetri.webapplication;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://vetbossel.in");

        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                try {
                    webView.stopLoading();
                } catch (Exception e) {
                }

                if (webView.canGoBack()) {
                    webView.goBack();
                }

                webView.loadUrl("about:blank");
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Error");
                alertDialog.setMessage("Check your internet connection and try again.");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        startActivity(getIntent());
                    }
                });

                alertDialog.show();
                super.onReceivedError(webView, errorCode, description, failingUrl);
            }
        });
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }
    }

    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
}

Execute the Application

Instead of execute on your device, I have added the screenshot file where I get output images. Through this you can easily understand the project output & how modules are presented on this example. Now you have test your application in android emulator. The exact output like,

Android webview webpage not available

If you have any doubt regarding this, feel free to comment below we are helps to solve issues. In the beginning is little bit hard so don’t lose your hopes. Android is the good domain for future. Every company hire android developers.

Leave a Reply