title: Offline Webview
body: <p>I have an app that takes a website and uses JavaScript to override some of the CSS to make it more mobile friendly. I am trying to add an offline mode and have not been able to get it to work. I have tried the following code but it has not worked.</p>
  
  <p>I added these permissions:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
  </code></pre>
  
  <p>App code:</p>
  
  <pre><code>package com.aquamorph.frcmanual;
  
  import android.annotation.SuppressLint;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebSettings;
  import android.webkit.WebSettings.RenderPriority;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  public class Introduction extends  Fragment  {
  
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {        
      super.onActivityCreated(savedInstanceState);
  }
  
  @SuppressWarnings("deprecation")
  @SuppressLint("SetJavaScriptEnabled")
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  
      View myFragmentView = inflater.inflate(R.layout.introduction, container, false);
  
      final ProgressDialog pd = ProgressDialog.show(getActivity(), "FRC Manual", "Loading");
      final WebView webView = (WebView) myFragmentView.findViewById(R.id.webview1);
      String url = "http://frc-manual.usfirst.org/viewItem/178";
  
      webView.getSettings().setJavaScriptEnabled(true);
      webView.setVerticalScrollBarEnabled(false);
      webView.setHorizontalScrollBarEnabled(false);
      webView.getSettings().setLoadWithOverviewMode(true);
      webView.getSettings().setUseWideViewPort(true);
      webView.getSettings().setRenderPriority(RenderPriority.HIGH);
  
      webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
      webView.getSettings().setAppCachePath("/data/data/de.app/cache");
      webView.getSettings().setAllowFileAccess( true );
      webView.getSettings().setAppCacheEnabled( true );
      webView.getSettings().setJavaScriptEnabled( true );
      webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default
  
      if ( !isNetworkAvailable() ) { // loading offline
          webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
      }
  
      webView.loadUrl(url);
  
  
  
      webView.setWebViewClient(new MyWebViewClient() {
       @Override
      public void onPageFinished(WebView view, String url)
      {
           webView.loadUrl("javascript:var con = document.getElementById('header_container'); " +
                   "con.style.display = 'none'; javascript:var con = document.getElementById('footer'); " +
                   "con.style.display = 'none'; javascript:var con = document.getElementsByClassName('ChapterTools RightPos')[0].style.visibility='hidden';"
                   + "javascript:var con = document.getElementsByClassName('colmid')[0].style.right='100%'; "
                   + "javascript:var con = document.getElementsByClassName('colleft')[0].style.right='initial'; "
                   + "javascript:var con = document.getElementsByClassName('col1')[0].style.width='98%'; "
                   + "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.right='2%'; "
                   + "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.width=''; "
                   + "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i &lt; img.length; ++i) {img[i].style.maxWidth='100%';} "
                   + "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i &lt; img.length; ++i) {img[i].style.height='';} "
                   + "javascript:var img = document.getElementsByTagName('table'); for (var i = 0; i &lt; img.length; ++i) {img[i].style.width='';} ");
  
           pd.dismiss();
      }
      });
  
      return myFragmentView;
  
  
  }
  
  
  private class MyWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (Uri.parse(url).getHost().equals("www.example.com")) {
              // This is my web site, so do not override; let my WebView load the page
              return false;
          }
          // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
          startActivity(intent);
          return true;
      }
  }
  private boolean isNetworkAvailable() {
      ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
      return activeNetworkInfo != null;
  }
  
  
  }
  </code></pre>