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?
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.
There are two ways to achieve it
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With