1

Possible Duplicate:
Android: ProgressDialog.show() crashes with getApplicationContext

I want to have a progress dialog to show up first then the toast. I want the progress dialog to load as long as the time delay chosen by the user like 15, 30, 60 seconds and no delay then the toast indicating that the message has been sent. How can I implement it? Where and how do I do it on my code?

Here's my code:

btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 
                boolean split = false;

                final Toast toast = Toast.makeText(getBaseContext(), 
                         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                          Toast.LENGTH_SHORT);

                Runnable showToastRunnable = new Runnable() {
                  public void run() {
                      toast.show();
        // Send button Listener

                  }
              };

                if (phoneNo.length()>0 && message.length()>0)  {
                    if (count == 0) {
                          handler.postDelayed(showToastRunnable, 0);
                      }
                      else if (count == 1) {
                          handler.postDelayed(showToastRunnable, 15000);
                      }
                      else if (count == 2) {
                          handler.postDelayed(showToastRunnable, 30000);
                      }
                      else if (count == 3) {
                          handler.postDelayed(showToastRunnable, 60000);
                      }
                }
                   // sendSMS(phoneNo, message, split); */
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }
halfer
  • 19,471
  • 17
  • 87
  • 173
kev
  • 155
  • 1
  • 10

1 Answers1

2

You can show progress dialog using following code

private ProgressDialog dialog;
public  void showProgress () {
    dialogSet = true;
    // prepare the dialog box
    //ProgressDialog 
    dialog = new ProgressDialog(this);
    // make the progress bar cancelable
    dialog.setCancelable(true);
    // set a message text
    dialog.setMessage("Please wait..");
    // show it
    dialog.show();

}

For cancelling the dialog, you should use dialog.cancel(). After cancelling dialog, you can display Toast

Change your code like this

Runnable showToastRunnable = new Runnable() {
              public void run() {
                  dialog.cancel();
                  toast.show();
              // Send button Listener
              }
             };
            if (phoneNo.length()>0 && message.length()>0)  {                                    
                showProgress ();    
                if (count == 0) {
                      handler.postDelayed(showToastRunnable, 0);

                  }
                  else if (count == 1) {
                      handler.postDelayed(showToastRunnable, 3000);

                  }
                  else if (count == 2) {
                      handler.postDelayed(showToastRunnable, 30000);
                  }
                  else if (count == 3) {
                      handler.postDelayed(showToastRunnable, 60000);
                  }
            }
               // sendSMS(phoneNo, message, split); */
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }
Sandy
  • 7,312
  • 6
  • 31
  • 39
  • I mean I want to have a progress dialog when the user taps on the send button. Example, the user chose a 15 second delay when he taps on the send button the progress dialog will show after 15seconds the toast will show that the message has been sent. – kev Nov 08 '11 at 06:04
  • To do that first call `showProgress()` which will show progress dialog then after call method for delay 15 sec, after 15 sec delay just cancel the dialog and display toast. – Sandy Nov 08 '11 at 06:14
  • Can you show it to me using my code? from above? and applying your code in it? – kev Nov 08 '11 at 06:26
  • The dialogSet cannot be resolved to a variable – kev Nov 08 '11 at 07:25
  • Just delete that statement, its a boolean variable – Sandy Nov 08 '11 at 08:17
  • There's a problem, the progress dialog won't show up – kev Nov 08 '11 at 08:23
  • Are you getting toast display? – Sandy Nov 08 '11 at 08:33
  • I have tested this. It works fine – Sandy Nov 08 '11 at 08:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4797/discussion-between-sandy-and-kev) – Sandy Nov 08 '11 at 08:39