12

I have a problem with toasts. For API 26 and below toasts are displayed properly (next toast is waiting for previous to disappear) but on Android 8.1 (API 27) they are covering each other. I have notification channel set up like this:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, 
                        NOTIFICATION_CHANNEL_NAME, 
                        NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(notificationChannel);
    builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}

This fixes toasts on 8.0 for me, but on 8.1 they are still overlapping like on this picture

Is there any way to fix this instead of remembering last used toast and canceling it manually?

Edit:

Workaround from this thread doesn't work

/**
 * <strong>public void showAToast (String st)</strong></br>
 * this little method displays a toast on the screen.</br>
 * it checks if a toast is currently visible</br>
 * if so </br>
 * ... it "sets" the new text</br>
 * else</br>
 * ... it "makes" the new text</br>
 * and "shows" either or  
 * @param st the string to be toasted
 */

public void showAToast (String st){ //"Toast toast" is declared in the class
    try{ toast.getView().isShown();     // true if visible
        toast.setText(st);
    } catch (Exception e) {         // invisible if exception
        toast = Toast.makeText(theContext, st, toastDuration);
        }
    toast.show();  //finally display it
}

toasts still overllaping

Edit 2: I've created story for this bug on Android Issue Tracker: link

Kacper Opyrchał
  • 349
  • 3
  • 13
  • Possible duplicate of [How to avoid a Toast if there's one Toast already being shown](https://stackoverflow.com/questions/6925156/how-to-avoid-a-toast-if-theres-one-toast-already-being-shown) – Max90 Apr 27 '18 at 13:23
  • Yeah, this is a bug on 7.1 I think. Thanks for posting. – Martin Marconcini May 04 '18 at 16:22
  • what is the relationship between notification channel and Toast in your question? – ygngy May 04 '18 at 16:26
  • @WIZARD Since Android 8.0 and above you need to setup proper notification channel to avoid developer warning toast (a potential problem in espresso automation tests). More here: https://stackoverflow.com/questions/44489657/android-o-reporting-notification-not-posted-to-channel-but-it-is – Kacper Opyrchał May 04 '18 at 16:54
  • I know channel is for notification but i think it is not relevant to `Toast` as you do not use channel id in your toast. **system notifies your mistake about channel with a toast but it do not mean that channel is needed for toast show**. is channel needed for `Toast`? – ygngy May 04 '18 at 18:30
  • You're right, notification channel is not necessary for toasts to work correctly. For me, toasts were completely broken without proper channel due to warning pop-up after every toast display (API 26). This is why I mentioned this, my bad. – Kacper Opyrchał May 04 '18 at 19:05

1 Answers1

1
private static final int TIME_DELAY = 4000;
private static long lastToastShowTime = 0;

showToast(final String msg, final Context ctx){
    final long pastTime = System.currentTimeMillis() - lastToastShowTime;
    if(pastTime > TIME_DELAY ){

        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        lastToastShowTime = System.currentTimeMillis();

     }else{
        final long delay = TIME_DELAY - pastTime;
        lastToastShowTime = System.currentTimeMillis() + delay;
        postDelayed(new Runnable(

            @Override
            public void run() {
               try{
                  Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
               catch(Exception e){
                  Log.e("TOAST_NOT_SHOWED", "Toast not showed: " + msg, e);
               }

            }

        ), delay);

    }
}
ygngy
  • 3,630
  • 2
  • 18
  • 29