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();
}
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.