How to load a custo...
 
Share:
Notifications
Clear all

[Solved] How to load a custom error page in webview when no internet connection?

  

0
Topic starter

I'm trying to find out how I add a custom offline page to my webview app. The app works well I just feel like a custom offline page would look much better! I have looked at some answers online but they don't seem to work any more. I hope someone can help! I want to show no internet page or no internet dialog.

Topic Tags
1 Answer
3

Using this code you can add no internet connection dialog. And also when click on Try again it checks connection, when internet connection available webview will load. if not show the dialog again.

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

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

Thanks problem solved. ❤️