4

In one of my methods, I have a toast that appears if the user gives the correct input. However, I do not want the next image to display until the toast has finished.

If I use Thread.sleep(3000) if does not allow the toast to show as the UI activity is asleep.

An example of what I am trying to do:

public void correction(){
        if(correctionBoolean == true){  
            Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();    
            if(Toast.time == finished){
            NextImage();}
            }
Navigatron
  • 2,065
  • 6
  • 32
  • 61

3 Answers3

5

I don't believe there would be any way to do this with a toast. If you are simply trying to show someone a "You're Correct" Window, I would consider simply using an AlertDialog with a single positive Okay button.

It would even be possible to show a dialog with no buttons, have a non-UI thread sleep for a bit and then dismiss the dialog.

Maximus
  • 8,351
  • 3
  • 29
  • 37
  • Ye I used an `AlertDialog` to display "correct", and force the user to dismiss the dialog. The sleep part cannot be done, as this method is located in the UI Thread. However the sleep timer was only used to prevent the `toast` from showing in the next image on the screen. By using an `AlertDialog`, this has prevented the `toast` overlap from occurring. – Navigatron May 28 '11 at 18:59
  • 1
    You still could do it with a sleep. A common practice, for instance with a progress dialog, is to us the AsyncTask Class to first display a dialog on the UI thread (onPreExecute), then do some work (or just sleep perhaps) on a background thread (doInBackground), then when complete and dismiss the dialog (onPostExecute). – Maximus May 28 '11 at 19:15
4

Create a custom dialog with no buttons and use a handler to both dismiss it after a short time and then show the next image.

Ben Williams
  • 6,027
  • 2
  • 30
  • 54
  • Thanks, but I just need something basic. I just created an `AlertDialog` instead of a toast, to stop overlapping of the toast onto the next image. – Navigatron May 28 '11 at 19:03
1

Use a CountDownTimer with Toast.LENGTH_SHORT as the time?

public void correction(){
    if(correctionBoolean == true){  
        Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();    
        new CountdownTimer(Toast.LENGTH_SHORT, 1000) {

            public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            NextImage();
        }
        }.start();

}
Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • 2
    Toast.LENGTH_SHORT is a constant value of 0, not an actual time, so I don't think that would work. – Geobits May 28 '11 at 18:45
  • 1
    Ah ok, didn't know that. Worth a shot! – Alex Curran May 28 '11 at 19:51
  • 1
    if you look here http://stackoverflow.com/questions/7965135/what-is-the-duration-of-a-toast-length-long-and-length-short you can find the length of these constant values. And have this actually work. – jbenowitz May 16 '13 at 22:58