Tuesday, November 15, 2011

Android Loading Dialog Box

By this you can show a loading dialog box to the user while performing a background task


First declare the ProgressBar and its Handler:


    ProgressDialog progressBarDialog;
     Handler handler=new Handler() {
             @Override
             public void handleMessage(Message msg)
             {
                
                 if(progressBarDialog != null)
                 {
                     progressBarDialog.dismiss();
                
                        ///Write the code u want to perform after the previous task was completed
                 }
                
             }
         };
   

Place the task/function/code u want to perform inside run()

progressBarDialog = ProgressDialog.show(PhoneLog.this, "Cyber Sports", "Saving...", true);
                        new Thread()
                        {
                            public void run()
                            {
                                GenerateResponse();///example of the task u want to perform
                                handler.sendMessage(handler.obtainMessage());
                            }
                        }.start();

And its done!!

No comments:

Post a Comment