/**
         * Removes the {@link Crouton}'s view after it's display
         * DurationInMilliseconds.
         *
         * @param crouton
         *     The {@link Crouton} added to a {@link ViewGroup} and should be
         *     removed.
         */

        public void RemoveCrouton(Crouton crouton)
        {
            View croutonView       = crouton.GetView();
            var  croutonParentView = (ViewGroup)croutonView.Parent;

            if (null != croutonParentView)
            {
                croutonView.StartAnimation(crouton.GetOutAnimation());

                // Remove the Crouton from the queue.
                var removed = (Crouton)croutonQueue.Poll();

                // Remove the crouton from the view's parent.
                croutonParentView.RemoveView(croutonView);
                if (null != removed)
                {
                    removed.DetachActivity();
                    removed.DetachViewGroup();
                    if (null != removed.GetLifecycleCallback())
                    {
                        removed.GetLifecycleCallback().OnRemoved();
                    }
                    removed.DetachLifecycleCallback();
                }

                // Send a message to display the next crouton but delay it by the out
                // animation duration to Make sure it finishes
                SendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.GetOutAnimation().Duration);
            }
        }
        private void CallOnGlobalLayout(Crouton crouton, View croutonView)
        {
            var layoutListener = new GlobalLayoutListener();

            layoutListener.OnGlobalLayout(delegate
            {
                if (Build.VERSION.SdkInt < Build.VERSION_CODES.JellyBean)
                {
                    croutonView.ViewTreeObserver.RemoveGlobalOnLayoutListener(layoutListener);
                }
                else
                {
                    croutonView.ViewTreeObserver.RemoveOnGlobalLayoutListener(layoutListener);
                }

                if (crouton.GetInAnimation() != null)
                {
                    croutonView.StartAnimation(crouton.GetInAnimation());
                    AnnounceForAccessibilityCompat(crouton.GetActivity(), crouton.GetText());
                    if (Configuration.DURATION_INFINITE != crouton.GetConfiguration().DurationInMilliseconds)
                    {
                        SendMessageDelayed(crouton, Messages.REMOVE_CROUTON,
                                           crouton.GetConfiguration().DurationInMilliseconds + crouton.GetInAnimation().Duration);
                    }
                }
            });
        }
        /**
         * Sends a {@link Crouton} within a delayed {@link Message}.
         *
         * @param crouton
         *     The {@link Crouton} that should be sent.
         * @param messageId
         *     The {@link Message} id.
         * @param delay
         *     The delay in milliseconds.
         */

        private void SendMessageDelayed(Crouton crouton, Int64 messageId, long delay)
        {
            Message message = ObtainMessage((int)messageId);

            message.Obj = crouton;
            SendMessageDelayed(message, delay);
        }
        /**
         * Sends a {@link Crouton} within a {@link Message}.
         *
         * @param crouton
         *     The {@link Crouton} that should be sent.
         * @param messageId
         *     The {@link Message} id.
         */

        private void SendMessage(Crouton crouton, int messageId)
        {
            Message message = ObtainMessage(messageId);

            message.Obj = crouton;
            SendMessage(message);
        }
        private long CalculateCroutonDuration(Crouton crouton)
        {
            long croutonDuration = crouton.GetConfiguration().DurationInMilliseconds;

            croutonDuration += crouton.GetInAnimation().Duration;
            croutonDuration += crouton.GetOutAnimation().Duration;
            return(croutonDuration);
        }
 private void RemoveCroutonFromViewParent(Crouton crouton)
 {
     if (crouton.IsShowing())
     {
         var parent = (ViewGroup)crouton.GetView().Parent;
         if (null != parent)
         {
             parent.RemoveView(crouton.GetView());
         }
     }
 }
        /**
         * Adds a {@link Crouton} to the {@link ViewParent} of it's {@link Activity}.
         *
         * @param crouton
         *     The {@link Crouton} that should be added.
         */

        private void AddCroutonToView(Crouton crouton)
        {
            // don't Add if it is already showing
            if (crouton.IsShowing())
            {
                return;
            }

            View croutonView = crouton.GetView();

            if (null == croutonView.Parent)
            {
                ViewGroup.LayoutParams parameters = croutonView.LayoutParameters;
                if (null == parameters)
                {
                    parameters = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MatchParent,
                                                                  ViewGroup.LayoutParams.WrapContent);
                }
                // display Crouton in ViewGroup is it has been supplied
                if (null != crouton.GetViewGroup())
                {
                    ViewGroup croutonViewGroup = crouton.GetViewGroup();
                    if (ShouldAddViewWithoutPosition(croutonViewGroup))
                    {
                        croutonViewGroup.AddView(croutonView, parameters);
                    }
                    else
                    {
                        croutonViewGroup.AddView(croutonView, 0, parameters);
                    }
                }
                else
                {
                    Activity activity = crouton.GetActivity();
                    if (null == activity || activity.IsFinishing)
                    {
                        return;
                    }
                    HandleTranslucentActionBar((ViewGroup.MarginLayoutParams)parameters, activity);
                    HandleActionBarOverlay((ViewGroup.MarginLayoutParams)parameters, activity);

                    activity.AddContentView(croutonView, parameters);
                }
            }

            croutonView.RequestLayout(); // This is needed so the animation can use the measured with/height
            ViewTreeObserver observer = croutonView.ViewTreeObserver;

            if (null != observer)
            {
                CallOnGlobalLayout(crouton, croutonView);
            }
        }
        /**
         * Removes a {@link Crouton} immediately, even when it's currently being
         * displayed.
         *
         * @param crouton
         *     The {@link Crouton} that should be removed.
         */

        public void RemoveCroutonImmediately(Crouton crouton)
        {
            // if Crouton has already been displayed then it may not be in the queue (because it was popped).
            // This ensures the displayed Crouton is removed from its parent immediately, whether another instance
            // of it exists in the queue or not.
            // Note: crouton.IsShowing() is false here even if it really is showing, as croutonView object in
            // Crouton seems to be out of sync with reality!
            if (null != crouton.GetActivity() && null != crouton.GetView() && null != crouton.GetView().Parent)
            {
                ((ViewGroup)crouton.GetView().Parent).RemoveView(crouton.GetView());

                // remove any messages pending for the crouton
                RemoveAllMessagesForCrouton(crouton);
            }
            // remove any matching croutons from queue
            IIterator croutonIterator = croutonQueue.Iterator();

            while (croutonIterator.HasNext)
            {
                var c = croutonIterator.Next().JavaCast <Crouton>();
                if (c.Equals(crouton) && (null != c.GetActivity()))
                {
                    // remove the crouton from the content view
                    RemoveCroutonFromViewParent(crouton);

                    // remove any messages pending for the crouton
                    RemoveAllMessagesForCrouton(c);

                    // remove the crouton from the queue
                    croutonIterator.Remove();

                    // we have found our crouton so just break
                    break;
                }
            }
        }
        /**
         * Inserts a {@link Crouton} to be displayed.
         *
         * @param crouton
         *     The {@link Crouton} to be displayed.
         */

        public void Add(Crouton crouton)
        {
            croutonQueue.Add(crouton);
            DisplayCrouton();
        }
 private void RemoveAllMessagesForCrouton(Crouton crouton)
 {
     RemoveMessages(Messages.ADD_CROUTON_TO_VIEW, crouton);
     RemoveMessages(Messages.DISPLAY_CROUTON, crouton);
     RemoveMessages(Messages.REMOVE_CROUTON, crouton);
 }
        /**
         * Allows hiding of a previously displayed {@link Crouton}.
         *
         * @param crouton
         *     The {@link Crouton} you want to Hide.
         */

        public static void Hide(Crouton crouton)
        {
            crouton.Hide();
        }