locked
How to convert AlertDialog to Snackbar? RRS feed

  • Question

  • User367459 posted

    I have the following in my Xamarin.Forms project:

    `if (ShouldShowRequestPermissionRationale(permission)) { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.SetTitle("Location permission needed"); alert.SetPositiveButton("Allow", (senderAlert, args) => { RequestPermissions(PermissionsGroupLocation, RequestLocationId); });

                alert.SetNegativeButton("Cancel", (senderAlert, args) =>
                {
                    Toast.MakeText(this, "Cancelled", ToastLength.Short).Show();
                });
    
                Dialog dialog = alert.Create();
                dialog.Show();
    
    
                return;
            }`
    

    I'd like to make it into a snackbar so it doesn't block the main page and dismisses itself after a few seconds

    The main problem is that Snackbar.Make() takes a view as its first parameter but I'm using Xamarin Forms

    Thursday, May 31, 2018 11:53 AM

Answers

  • User42420 posted

    In case you need elaborate answer. Follow the steps :

    1. Create an interface(PCL)

                                             public interface ISnackBarNotification 
                                            {
                                                void NotifyUserWithAlert(string message, int duration, string action, Action<object> callback);
                                            }
    

    2. Implement this interface in your project

                 [assembly: Dependency(typeof(YourProjectNameSpace.SnackBarAlertService))]
                                    namespace YourProjectNameSpace
                                    {
                                        public class SnackBarAlertService : ISnackBarNotification
                                        {
                                            public void NotifyUserWithAlert(string message, int duration, string actionText, Action<object> action)
                                            {
                                                var view = ((Activity)Forms.Context).FindViewById(Android.Resource.Id.Content);
                                                var snack = Snackbar.Make(view, message, duration);
                                                if (actionText != null && action != null)
                                                    snack.SetAction(actionText, action);
                                                snack.Show();
                                            }
                                        }
                                    }
    

    Finally in your PCL project, call this DependencyService

    DependencyService.Get<ISnackBarNotification>() .NotifyUserWithAlert("Hello there!!", 5000, "OK", obj => undo());

    Hope this Helps!!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 31, 2018 12:46 PM

All replies

  • User42420 posted

    I think you should go with a cleaner/better approach, i.e, using DependencyServices

    Thursday, May 31, 2018 12:21 PM
  • User367459 posted

    How can I use DependencyServices?

    Thursday, May 31, 2018 12:35 PM
  • User42420 posted

    In case you need elaborate answer. Follow the steps :

    1. Create an interface(PCL)

                                             public interface ISnackBarNotification 
                                            {
                                                void NotifyUserWithAlert(string message, int duration, string action, Action<object> callback);
                                            }
    

    2. Implement this interface in your project

                 [assembly: Dependency(typeof(YourProjectNameSpace.SnackBarAlertService))]
                                    namespace YourProjectNameSpace
                                    {
                                        public class SnackBarAlertService : ISnackBarNotification
                                        {
                                            public void NotifyUserWithAlert(string message, int duration, string actionText, Action<object> action)
                                            {
                                                var view = ((Activity)Forms.Context).FindViewById(Android.Resource.Id.Content);
                                                var snack = Snackbar.Make(view, message, duration);
                                                if (actionText != null && action != null)
                                                    snack.SetAction(actionText, action);
                                                snack.Show();
                                            }
                                        }
                                    }
    

    Finally in your PCL project, call this DependencyService

    DependencyService.Get<ISnackBarNotification>() .NotifyUserWithAlert("Hello there!!", 5000, "OK", obj => undo());

    Hope this Helps!!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 31, 2018 12:46 PM