Progress Dialog in android

November 3, 2012 | Rishabh Agrawal | General

A 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 suitablr 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:

 


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal