Toast message for Xamarin Forms

toastdemo_androidtoastdemo_ios

Quick and easy solution for toast messages in Xamarin Forms, it supports iOS and Android platforms. Toast message provides simple feedback in a small popup (which is disappearing in some seconds) like on the screenshots on the left (Android) and the right (iOS) sides.


First, let’s create interface (file ToastDemo/Interfaces/IMessage.cs):

public interface IMessage
{
    void LongAlert(string message);
    void ShortAlert(string message);
}

Then we are creating dependency service in Android (Droid/Native/Toast.cs) and implementing IMessage interface there using native Android toasts:

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();
    }
}

iOS does not have native toasts, let’s use UIAlert with custom automatic hide to simulate toast functionality (iOS/Native/Toast.cs):

public class MessageIOS : IMessage
{
    const double LONG_DELAY = 3.5;
    const double SHORT_DELAY = 2.0;
 
    NSTimer alertDelay;
    UIAlertController alert;
 
    public void LongAlert(string message)
    {
        ShowAlert(message, LONG_DELAY);
    }
    public void ShortAlert(string message)
    {
        ShowAlert(message, SHORT_DELAY);
    }
 
    void ShowAlert(string message, double seconds)
    {
        alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
        {
            dismissMessage();
        });
        alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
    }
    void dismissMessage()
    {
        if (alert != null)
        {
            alert.DismissViewController(true, null);
        }
        if (alertDelay != null)
        {
            alertDelay.Dispose();
        }
    }
}

Now let’s simplify calling toast messages by wrapping dependency services with the static methods (ToastDemo/Helpers/Toast.cs):

public static class XFToast
{
    public static void ShortMessage(string message)
    {
        DependencyService.Get<IMessage>().ShortAlert(message);
    }
 
    public static void LongMessage(string message)
    {
        DependencyService.Get<IMessage>().LongAlert(message);
    }
}

That’s it! Now you can show the toast by calling anywhere in the project the method XFToast.ShortMessage() or XFToast.LongMessage().

Ready to play demo project is available on github.com/bykovme/toastdemo

, , , , , , , , , ,

  1. #1 by Anup on January 31, 2017 - 11:59

    Hello ,
    i have already implement Toast Message in the above Given methods since my requirement was only for Android & IOS now i have too include UWP Platform also how to achieve Toast Message this using same Approach..pls help

    Liked by 1 person

  2. #3 by Bharathi Boga on March 4, 2017 - 10:12

    Thanks for sharing.its help for me.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: