I'm out to do something when no connection is available page/alert in WebView
(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.
Asked
Active
Viewed 1.2k times
5
4 Answers
6
It all came down to simply showing an AlertDialog from onReceivedError:
@Override
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
//Clearing the WebView
try {
webView.stopLoading();
} catch (Exception e) {
}
try {
webView.clearView();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
//Showing and creating an alet dialog
AlertDialog alertDialog = new AlertDialog.Builder(youractivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("No internet connection was found!");
alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
//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
}
});

Mdlc
- 7,128
- 12
- 55
- 98
4
The above code gives me two deprecation warnings, so I would suggest modifying it as follows. This goes in the activity that contains the WebView component:
myWebView.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("Cannot connect to the R2R Server. 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);
}
});

haobole
- 41
- 1
0
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// declare a text view in your xml
sample_TextView.setText(R.string.no_internet_connection);
view.loadUrl("about:blank");
}

Zub
- 808
- 3
- 12
- 23
-
Hey dude, I saw that you've made some edits, but you only edit minor things like uppercasing the first word in the title, you should also fix other problems a question has. I elaborate this on one edit you've done to [this question](https://stackoverflow.com/q/47450223), in this question you also could uppcase the I and Tessaract and remove the empty lines from the code and remove the unnessesary `...`. This is just a friendly reminder, but sooner or later, edits like this will be rejected. – jmattheis Nov 24 '17 at 18:15
0
You can load html file from assets. Put your html like below. assets/html/no_connection.html
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
{
Toast.makeText(getActivity(),String.valueOf(error.getErrorCode())+":"+ error.getDescription(),Toast.LENGTH_LONG).show();
wv.loadUrl("file:///android_asset/html/no_connection.html");
super.onReceivedError(view, request, error);
}
The following one is deprecated in api 23
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// TODO: Implement this method
super.onReceivedError(view, errorCode, description, failingUrl);
}
Forgive me if something went wrong. I am not proficient.

nay thurein
- 11
- 1