Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is so simple. The basic fact behind this is as follows.</p> <p>Your progress dialog runs in the main UI. Your <code>for</code> loop here gets executed so soon and takes more priority and hence you are not able to see your progress dialog but the fact is, your code works fine and progress dialog does show up for fraction of seconds which the human eye can't catch.</p> <p>Usually people use a progress dialog when they do something in the background thread and not the main thread. So you will have to change a little bit of your code. They are,</p> <p>1) Surround your <code>for</code> loop with a thread like this,</p> <pre><code> Button end = (Button) findViewById(R.id.button2); end.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ProgressDialog pd = new ProgressDialog(ZiiziiActivity.this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("Working..."); pd.setIndeterminate(true); pd.setCancelable(false); pd.show(); Thread t = new Thread(new Runnable() { @Override public void run() { for (int i=0;i&lt;1000000;i++) { Thread.sleep(100); //To denote a delay in background processing } handler.sendEmptyMessage(0); } }); t.start(); } }); </code></pre> <p>2) Now add a handler to your onCreate() like this:</p> <pre><code> handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0) { pd.dismiss(); } }; </code></pre> <p>Note that you can't update your UI from a background thread. So in order to update the UI from some other worker thread you go for handlers. So once your for loop gets completed, a call will be made to handlers which updates the UI. (In this case it cancels the progress dialog).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload