0

Is there a way to style a toast in xamarin forms? like changing the corner radius or background color? at least for android. I am using dependency service to show the toasts an it works fine but I prefer to style the toast a bit. Thanks in advance.

Andorid toast:

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))]
namespace DeliveryApplication.Droid.DependencyService
{
    class MessageAndroid : IMessage
    {
        public void LongToast(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
        }

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

Ios Toast:

[assembly: Xamarin.Forms.Dependency(typeof(MessageIOS))] 
namespace DeliveryApplication.Droid.DependencyService
{

    class MessageIOS : IMessage
    {
        const double LONG_DELAY = 3.5;
        const double SHORT_DELAY = 2.0;

        NSTimer alertDelay;
        UIAlertController alert;
        public void LongToast(string message)
        {
            ShowAlert(message, LONG_DELAY);
        }

        public void ShortToast(string message)
        {
            ShowAlert(message, SHORT_DELAY);
        }

        private 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();
            }
        }
    }
}
Sam
  • 291
  • 3
  • 17
  • The question linked as duplicate handles native Android case only, where the op seems to be asking about iOS too. – Cfun Apr 18 '21 at 16:47
  • he said "at least for android". If he wants to solve for iOS also then he can search for "custom UIAlertController" – Jason Apr 18 '21 at 17:39

0 Answers0