title: how to sleep in for loop until finished loading page android
body: <p>I am making an app to do something in webview automatically.</p>
  
  <p>I want to make a pause between two lines inside (for-loop) until page finished loading without using Thread.sleep because it freezing my application.</p>
  
  <p>this is my code:</p>
  
  <pre><code>    webview.loadUrl("http://**********");
  
      button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
  
  
                  for(int i=1;i&lt;10;i++){
  
                           evaluateJavascript( "document.getElementById('select').value=" + i)
                           evaluateJavascript("document.getElementById('Search').click();")
  
                           //wait until finished loading
                           while( isloading() ){
                               try {
                                    Thread.sleep(1000);
                                   } catch (InterruptedException e) {
                                    e.printStackTrace();
                                   }
                           }
  
  
                           evaluateJavascript( "document.getElementById('any_select').value=5")
                           .
                           .
                           .
                           .
  
                  }
  
  
  
  
  public boolean isloading(){
  boolean isloading;
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
              webview.evaluateJavascript("(function() { return document.getElementById(\"Loading\").style.display; })();", new ValueCallback&lt;String&gt;() {
                  @Override
                  public void onReceiveValue(String s) {
                      if(s.equals("none")){
                      isloading=false;
                      }else{
                      isloading=true;
                      }
                  }
              });
      }
      if(isloading=true)return true;
      if(isloading=false)return false;
  
  
  }
  </code></pre>