title: Show ProgressBar for a certain time in Android
body: <p>I have to wait some seconds in my Android App and I want to show a progress bar during this time, how can I do this?</p>
  
  <p>I tried for example this code:</p>
  
  <pre><code>    public boolean WaitTask() {
  
          pDialog = ProgressDialog.show(context,null, "Lädt..",true);
          new Thread() {
          public void run() {
              try{
                  // just doing some long operation
                  sleep(2000);
               } catch (Exception e) {  }
               pDialog.dismiss();
               }
           }.start();
          return true;
     }
  </code></pre>
  
  <p>But the progressbar closes immediately without waiting the two seconds. Where is my problem?</p>
  
  <p>The progressbar should look like the activity circle showing in <a href="http://developer.android.com/design/building-blocks/progress.html">this</a> site from Android Developers.</p>
  
  <p><strong>UPDATE</strong>
  The AsyncTask</p>
  
  <pre><code>private class WaitTime extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          mDialog.show();
      }
      protected void onPostExecute() {
          mDialog.dismiss();
      }
  
  @Override
  protected void onCancelled() {
      mDialog.dismiss();
      super.onCancelled();
  }
  
      @Override
      protected Void doInBackground(Void... params) {
          long delayInMillis = 2000;
          Timer timer = new Timer();
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  mDialog.dismiss();
              }
          }, delayInMillis);
          return null;
      }
  }
  </code></pre>
  
  <p>I call it like this:</p>
  
  <pre><code>        mDialog = new ProgressDialog(CreateProject.this);
          mDialog = ProgressDialog.show(context,null, "Lädt..",true);
  
          WaitTime wait = new WaitTime();
          wait.execute();
  </code></pre>