Showing ProgressDialog in an Activity

Posted on Updated on

In some cases we need to wait until some long process is carried out in an activity. In this case its a good practice to notify user that the operation is in progress and also for preventing the UI from freezing. For this purpose android API has got classes such as ProgressDialog and ProgressBar. Here I am going to explain one of the method that is how to use ProgressDialog in an Activity.

The syntax for ProgressDialog is 

    ProgressDialog.show(Context context, CharSequence title, CharSequence message);


Now we need to create thread for the complex operation. The entire code is shown below


private ProgressDialog progressDialog;
progressDialog=ProgressDialog.show(Classname.this, "Please Wait", "Executing complex operation....");
new Thread()
{
public void run()
{
try
{
sleep(1000);
//add ur complex function here
}
catch(Exception e)
{
}
progressDialog.dismiss();
}
}.start();

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