Forum Xamarin.Forms

toast that wait for an exist toast to disapear

EliranEliran USMember ✭✭

Hi,

My problem is that I created a toast that works fine, but unfortunately, toasts are not very polite, and don't not wait until the current shown toast will
finish its job. It just moves it away.
I can solve it in an ugly way, which means to put Task.Delay() before the second toast (in order to let the first toast to finish its job).

I wondered if there is a better way.

This is how I created the toast.
1. I created an interface in Xamarin Forms:
public interface IMessage
{
void LongAlert(string message);
void ShortAlert(string message);
}

  1. In Android Project I wrote the implementation:

    public class MessageAndroid : IMessage
    {
    public void LongAlert(string message)
    {
    Toast.MakeText(Application.Context,message, ToastLength.Long).Show();
    }

        public void ShortAlert(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Short).Show();
        }
    

    }

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    The behavior of Toast has been changed since Android 8.1.
    What you want is the prior effect: the Toast will be triggered one by one just like in the same queue.
    But the Toast will be covered by the later one on Android 8.1+.
    If you do want the previous effect I'm afraid you have to utilize the Task.Delay to achieve that.

  • NMackayNMackay GBInsider, University mod

    You can also achieve toasts using Rg.Plugins creatively and you have control over the UX behaviour.

  • EliranEliran USMember ✭✭

    @LandLu said:
    But the Toast will be covered by the later one on Android 8.1+.

    Hi LandLu, thanks for your answer. I'm using Android 6.0.1 and unfortunately, also in this version,
    the first toast is covered by the next one.
    I see the toast for a second, and then before I manage to read all the message, the next toast arrives and hides the previous one :-(

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Eliran Well, it must be system related. I tested on my 8.0, 8.1 and 9.0 devices, the 8.0 pixel will display toast one by one however the others overlap the previous toasts.

  • EliranEliran USMember ✭✭

    @NMackay said:
    You can also achieve toasts using Rg.Plugins creatively and you have control over the UX behaviour.

    HI NMackay , Thank you for your answer. I didn't know Rg.Plugins before. It is pretty good for animation goals, but it doesn't solve my problem, because it doesn't have an option to set waiting until the current popup finish.
    In spite of that, it has a property which tells us how many popup are in the air right now, so I can check if there is at least 1 pop up in the air, and then I will wait, but this brings me back to the solution of Task.Deleay().

    It seems there is no other way, than just waiting with some arbitrary time. I wish there was some event that tells us
    when the toast has finished its job, without the need to wait and check randomly.

  • AravindakumarAravindakumar Member ✭✭

    Hi @Eliran,

    You can save the toast status in a App global variable or preferences.

    Based on the status you can hide/show the toast message.

    For example: If the current status of alert message is TRUE => An alert already shown to the user and not yet closed.

    if it is false then you can populate the new one.

    Just an idea. Hope it helps.

Sign In or Register to comment.