Search code examples
androiddelayprogressdialogtoast

Android: ProgressDialog then toast


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();
            }
        });        
    }

Solution

  • 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();
            }