Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a custom "webpage not available" page in Android WebView?

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.

like image 654
Mdlc Avatar asked Dec 26 '12 11:12

Mdlc


People also ask

How do I display HTML content in WebView?

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.

What is alternative of WebView in Android?

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.

Do cookies work in WebView?

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.

How do I enable JavaScript on Android WebView?

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.


1 Answers

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
    }
});
like image 180
Mdlc Avatar answered Nov 09 '22 06:11

Mdlc