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.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.
Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.
Cookie From API Service to WebView By doing so, whenever a cookie is set by the API through the API call using the particular instance of okHttpClient , the cookie will be stored automatically and will be used by Webview launched by the App.
Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With