0

In my ContentView I have some offline content and also one WebView. When device is online, WebView shows the content from the web. But when device is offline, white page is shown instead. How can I get rid of this blank page? I would like not to display any WebView content in offline mode.

My code is:

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = cm
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo datac = cm
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if ((wifi != null & datac != null)
            && (wifi.isConnected() | datac.isConnected())) {
    android.webkit.WebView wv = 
    (android.webkit.WebView)this.findViewById(R.id.myWebView);
    wv.clearCache(true);
    wv.loadUrl("MyURL");
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    wv.getSettings().setJavaScriptEnabled(true);
    }
Art
  • 276
  • 1
  • 2
  • 17

1 Answers1

0

Well if I understand it correct you want it to show some errors instead of blank page. Then you can use the below code:

mainWebView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            String summary = "<html><h1>Could not connect to the server</h1><h2>May be you are not connected to Internet or</h2><h3>our server is down.</h3><body>Please exit the program and try again later.</body></html>";
            mainWebView.loadData(summary, "text/html", null);               

        }
    });

or if you want to go came back to the same activity when the device is offline instead of showing the blank page then just use this code:

                    mainWebView.onResume();
                    finish();

I had the same problem. So I displayed the error instead of blank page. and afterwards I decided to go back to the activity from where I started my web. so I used finish(); . this helps to finish the error and go back.

The source code that I used was.

mainWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                String summary = "<html><h1>Could not connect to the server</h1><h2>May be you are not connected to Internet or</h2><h3>our server is down.</h3><body>Please exit the program and try again later.</body></html>";
                mainWebView.loadData(summary, "text/html", null);

                Toast.makeText(activity, "Error. Cannot to the server. May be you are not connected to Internet or our server is down or " + description, Toast.LENGTH_LONG)
                        .show();
                mainWebView.onResume();
                    finish();



            }
        });

        mainWebView.loadUrl(getIntent().getStringExtra("url"));

P.S Sorry to reply late. I believe after 1year.