Progress Dialog in android

ProgressDialog is used for progress and  a text message .When we want the user to wait while some processing is taking place ,then we can use these dialogs. Example when some downloading is taking place we show a ProgressDialog until its complete.

It has two styles :

STYLE_HORIZONTAL-Creates a ProgressDialog with a horizontal progress bar.

STYLE_SPINNER-Creates a ProgressDialog with a circular, spinning progress bar.

Syntax:

ProgressDialog progressDialog;
ProgressDialog.show(  context,  "Title  of dialog",  "Loading (message)”); progressDialog.setCancelable(false);//can’t be cancelled on back press
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(100); // sets progress
incrementProgressBy(100);//int value used to progress ahead
progressDialog.show();//show dialog

We can implement a ProgressDialog using the follwing  three ways :

1. Using with handler

public class MainActivity extends Activity {
   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
        final ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         progressDialog.setCancelable(false);
progressDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
 @Override

  public void run() {
// this code runs after 4 sec
progressDialog.dismiss();                                                                                   }
}, 4000);
}
}

2. Using thread

public class SecondActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog dialog=new ProgressDialog(SecondActivity.this);
dialog.setTitle("Loading..");
dialog.setMessage("Downloading source..");
dialog.setProgress(100);
dialog.setMax(5000);
dialog.show();
Thread splashThread = new Thread() {
@Override
public void run()
{
try
{
sleep(5000);
} catch (Exception e)

{
e.printStackTrace();
}
finally {
finish();
}
}
};          /** END OF THREAD */
splashThread.start();
}
}

3. Using async tasks

private class Operation extends AsyncTask<
String, Void, String>
{           @Override
 protected String doInBackground(String... params)
{ // code to be executed in background thread
for(int i=0;i<5;i++) {

try {
Thread.sleep(1000);
} catch (InterruptedException e)
                 {      // TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
           }
@Override
protected void onPostExecute(String result)
 { // runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
           }
@Override
           protected void onPreExecute() {
 ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
               progressDialog.show();
           }
           @Override
protected void onProgressUpdate(Void... values) {
 // runs on UI thread and starts first
           }

}

Advantageof using handler is that it does’nt interfere with UI thread and are better than threads.

Whereas, Async Tasks are suitable for  long task having to communicate with main thread.

Hence , now you can implement ProgressDialog in three different ways  .You can also refer to ProgressDialogDemo project attached as reference.

ProgressDialogDemo

Screenshot:

Advertisements

3 thoughts on “Progress Dialog in android

  1. Pingback: Custom Dialog Boxes in Android Example | Androidexterous

  2. At this time it looks like Movable Type is the best blogging platform
    available right now. (from what I’ve read) Is that what you are using on your blog?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s