How to show a default image in webview if webpage cannot be loaded

2.9k views Asked by At

I have a WebView in a layout, which is basically loading whatever url given. Everything is working fine. But, when the page is unavailable for some reasons like if the WebView cannot access the server or if webpage is removed, the WebView shows, "Webpage not available. The webpage at www.givenurl_something.com might be temprarily down" etc etc. Instead of showing this text to users, I want to show a default image stored in the app itself inside the WebView. I mean when everything is working good, the webpage should open. Otherwise, a default image should be shown, can anybody help me? Below is my code.

public class WebviewActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.webview);
    cd = new ConnectionDetector(getApplicationContext());

    // Create ad request
    isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {

        WebView w1 = (WebView) findViewById(R.id.webView1);
        w1.setWebViewClient(new WebViewClient());
        w1.getSettings().setJavaScriptEnabled(true);
        w1.getSettings().setBuiltInZoomControls(true);

        w1.loadUrl("http://www.google.com");
        WebClientClass webViewClient = new WebClientClass();
        w1.setWebViewClient(webViewClient);
        WebChromeClient webChromeClient = new WebChromeClient();
        w1.setWebChromeClient(webChromeClient);
    }

    else {

        showAlertDialog(WebviewActivity.this, "No Internet Connection",
                "You don't have internet connection.", false);
    }
}

public class WebClientClass extends WebViewClient {
    ProgressDialog pd = null;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        pd = new ProgressDialog(WebviewActivity.this);
        pd.setTitle("Please wait");
        pd.setMessage("Page is loading..");
        pd.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        pd.dismiss();
    }

}

public class WebChromeClass extends WebChromeClient {
}

public void showAlertDialog(Context context, String title, String message,
        Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    // alertDialog.setIcon(R.id.action_mode_close_button);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}
2

There are 2 answers

0
Darpan On BEST ANSWER

Use this code to override your error page and load a custom page of your own. And keep this custom error page (myerrorpage.html) with your custom image in assets folder.

mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file:///android_asset/myerrorpage.html");

            }
        });
0
Shadab Mirza On

You can override the
onReceivedError (WebView view, int errorCode, String description, String failingUrl)
function of the WebViewClient class.
In this you can show your default image when the webview receives any error while loading a page. You can also check the error codes to load different images for different failure cases.

Refer the android docs for WebViewClient for details about the constant error codes: http://developer.android.com/reference/android/webkit/WebViewClient.html