Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with display multiple Toast in order one after another

Tags:

android

toast

sorry for my bad English.
i want show two toast in order, in other word when first toast duration is over second toast appear.
this is my code :

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Toast2", Toast.LENGTH_SHORT).show();

but only second toast message will appear. i think when show method of second toast will execute it will cancel previous toast (first toast)

I solved my problem with this code :

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
    Handler handler =new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Toast.makeText(MainActivity.this, "Toast2", Toast.LENGTH_SHORT).show();
        }
    },1000);

but is there any easier solution?

like image 961
danial abadi ghadim Avatar asked Sep 14 '18 14:09

danial abadi ghadim


3 Answers

but only second toast message will appear. i think when show method of second toast will execute it will cancel previous toast (first toast)

When you call show method, it will put into message queue of UI thread, and the Toast will be shown in order. But you put two Toast at the same time, the latter will overlap the former.

i want show two toast in order, in other word when first toast duration is over second toast appear.

From Toast duration

private static final int LONG_DELAY = 3500; // 3.5 seconds 
private static final int SHORT_DELAY = 2000; // 2 seconds

To make the second toast display after duration of the first one, change your code to

Toast.makeText(this, "Toast1", Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        Toast.makeText(MainActivity.this, "Toast2", Toast.LENGTH_SHORT).show();
    }
}, 2000);

but is there any easier solution?

Using Handler is the easy and simple solution to achieve your task.

like image 157
Son Truong Avatar answered Sep 24 '22 13:09

Son Truong


There are two ways to achieve it

  • Method 1: Use Thread as you used but use timer and execute one by one
  • Method 2: Use any Loop, For Example use For Loop
like image 32
Android Developer Avatar answered Sep 21 '22 13:09

Android Developer


Other solution is to use AlertDialog

createDialog().show();

with two methods createDialog() and continueDialog()

public AlertDialog createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Toast1")
            .setPositiveButton("Next",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            continueDialog().show();
                        }
                    });
    return builder.create();
}

public AlertDialog continueDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Toast2")
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
    return builder.create();
}
like image 36
Fahed Hermoza Avatar answered Sep 24 '22 13:09

Fahed Hermoza