title: how to webview offline and online load website content in android
body: <p>i am actually new to programming in java but i have been following several solutions to my problem here but didn't find one that suits my case and i can't seem to get the code down correctly.</p>
  
  <p>i would like to have a WebView which opens an online page (for example google) when the phone is online and open a local html page when the phone is offline.</p>
  
  <p>at the same time though i want the phone to overwrite the local page when it is online so that the offline local page is always updated to the last time the phone was connected to the internet.</p>
  
  <p>any ideas how this could be done? some simple pointing to the right direction could help.</p>
  
  <p>thanks a lot</p>
  
  <p>My Source Code </p>
  
  <pre><code>        WebView engine=(WebView)findViewById(R.id.webView);
          engine.getSettings().setJavaScriptEnabled(true);
          engine.getSettings().setBuiltInZoomControls(true);
          engine.getSettings().setLoadsImagesAutomatically(true);
                 engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
          engine.setInitialScale(1);
  
          engine.setWebChromeClient(new WebChromeClient() {
                @Override
                   public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,QuotaUpdater quotaUpdater)
                   {
                         quotaUpdater.updateQuota(spaceNeeded * 2);
                   }
                public void onProgressChanged(WebView view, int progress)
                {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
  
  
  
                    if(progress == 100)
                    { activity.setTitle(R.string.app_name);
  
                    }
                }
             });
          engine.getSettings().setDomStorageEnabled(true);
         // Set cache size to 8 mb by default. should be more than enough
          engine.getSettings().setAppCacheMaxSize(1);
          engine.getSettings().setAllowFileAccess(true);
          engine.getSettings().setAppCacheEnabled(true);
          engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
          Map&lt;String, String&gt; noCacheHeaders = new HashMap&lt;String, String&gt;(2);
          noCacheHeaders.put("Pragma", "no-cache");
          noCacheHeaders.put("Cache-Control", "no-cache");
        //  engine.loadUrl("http://www.stackhand.com", noCacheHeaders);
  
           cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
           if(cm.getActiveNetworkInfo().isConnected())
           {
           engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
  
           }
               else{
                  engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  
               }
  
  
  
           engine.loadUrl("http://www.stackhand.com", noCacheHeaders);
  </code></pre>
  
  <p>use this function checking network online and offline </p>
  
  <pre><code>   private boolean isNetworkAvailable() {
          ConnectivityManager connectivityManager = (ConnectivityManager)       
          getSystemService( CONNECTIVITY_SERVICE );
          NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
          return activeNetworkInfo != null &amp;&amp; activeNetworkInfo.isConnected();
    }
  </code></pre>