title: Calculating time it takes for a webpage to fully load in a webview
body: <p>I have an application that reads websites from a text file and then calculates the time it takes to fully load the webpage into the webview. I try to calculate the time by getting the time in milliseconds before I load the url into the webview and then get the time after the webpage has finished loading in the webview and taking the difference between the two, but it always comes out to 0. Any ideas why? The time it takes to load a webpage should be greater than 0?</p>
  
  <pre><code>public class MainActivity extends Activity {
      // NUMSITES will be the number of websites contained in the sites.txt file.
      int NUMSITES = 4;
      int curSite = 0;
      long startTime = 0;
      long endTime = 0;
      long totTime = 0;
      boolean start = true;
      Calendar cal = Calendar.getInstance();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_main);
      final WebView wb = (WebView) findViewById(R.id.webView1);
      final String[] sites = new String[NUMSITES];
      getWebsites(sites);
  
      // Listed as optimal settings for HTML5 (may need testing?).
      // Ref.
      // http://stackoverflow.com/questions/10097233/optimal-webview-settings-for-html5-support
      wb.getSettings().setJavaScriptEnabled(true);
      WebSettings settings = wb.getSettings();
      settings.setJavaScriptEnabled(true);
      settings.setDomStorageEnabled(true);
      wb.setFocusable(true);
      wb.setFocusableInTouchMode(true);
      wb.getSettings().setRenderPriority(RenderPriority.HIGH);
      wb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
      wb.getSettings().setDatabaseEnabled(true);
      wb.getSettings().setAppCacheEnabled(true);
      wb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      WebViewClient client = new WebViewClient() {
  
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              return true;
          }
  
          @Override
          public void onLoadResource(WebView view, String url) {
          }
  
          @Override
          public WebResourceResponse shouldInterceptRequest(WebView view,
                  String url) {
              return null;
          }
  
          // On page finished, retrive next website from string array and load
          // it.
          @Override
          public void onPageFinished(WebView view, String url) {
              //
              endTime = cal.getTimeInMillis();
              totTime = endTime - startTime;
              Log.d("Time", "Time: " + totTime);
              Log.d("Time", "Start Time: " + startTime);
              Log.d("Time", "End Time: " + endTime);
              if (curSite &lt; NUMSITES) {
                  startTime = cal.getTimeInMillis();
                  wb.loadUrl(sites[curSite]);
  
                  curSite++;
              }
          }
  
      };
  
      wb.setWebViewClient(client);
      // Initial webpage
      wb.loadUrl("https://sites.google.com/site/imagesizetesting/one-1");
  }
  </code></pre>