0

A friend is writing an Android app for school and I'm helping him a bit. There is one question that I was not able to solve for like an hour.

He is trying to show 2 toasts after each other, but we were not able to stack them, or even show them at the same time. All we see is the second Toast. We tried showing it for a shorter time, than the first, to see if it was hiding behind the second, it was not. Then we placed the second in the middle of the screen, but it didn't help either. He said it just works for his friend (I can't confirm that, but also couldn't google anyone having the same issue)

Toast t1 = Toast.makeText(getApplicationContext(), "first", Toast.LENGTH_LONG);
t1.show();

Toast t2 = Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT);
t2.setGravity(0, 50, 0);
t2.show();

Are we totally missing something? Is it even designed to show two toasts the same time, or stack them?

Basti
  • 606
  • 1
  • 12
  • 22
  • Which Android version is your friend testing on? There's been some odd behavior observed with `Toast`s in 8+. This is possibly related. You might have them try testing on an earlier version. – Mike M. Jun 06 '18 at 22:18

2 Answers2

1

Use postDelayed(). Show the first Toast and after sometime show the second one:

final Handler handler = new Handler();


    handler.postDelayed(
            () -> //show the toast here,
            1200);
        handler.postDelayed(() -> //show second toast,
     2400);
          }

https://developer.android.com/reference/android/os/Handler

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
  • looks like the most valid approach so far, too sad, they didn't make em stackable etc. Not sure if his friend is lying :P – Basti Jun 06 '18 at 20:14
-1

Try a snackbar (They look way better too!)

protected ArrayList<Snackbar> mSnackbarList = new ArrayList<>();

protected Snackbar.Callback mCallback = new Snackbar.Callback() {
    @Override
    public void onDismissed(Snackbar snackbar, int event) {
        mSnackbarList.remove(snackbar);
        if (mSnackbarList.size() > 0)
           displaySnackbar(mSnackbarList.get(0));
    }
};

public void addQueue(Snackbar snackbar){
    setLayoutParams(snackbar);
    snackbar.setCallback(mCallback);
    mSnackbarList.add(snackbar);
    if(mSnackbarList.size() == 1)
        displaySnackbar(snackbar);
}

public void displaySnackbar(Snackbar snackbar){
    snackbar.show();
}
TomH
  • 2,581
  • 1
  • 15
  • 30
  • hm looks like too much overhead for a school project, thanks for your contribution though! – Basti Jun 06 '18 at 20:15