Exemplo n.º 1
0
        /// <summary>
        /// ShowToastMessage
        /// </summary>
        /// <param name="message"></param>
        /// <param name="backgroundHexColor"></param>
        private void ShowMessage(string message, string backgroundHexColor = null, string textHexColor = null, Abstractions.ToastLength toastLength = Abstractions.ToastLength.Short)
        {
            var length = toastLength == Abstractions.ToastLength.Short ? Android.Widget.ToastLength.Short :  Android.Widget.ToastLength.Long;

            // To dismiss existing toast, otherwise, the screen will be populated with it if the user do so
            _instance?.Cancel();
            _instance = Android.Widget.Toast.MakeText(Android.App.Application.Context, message, length);

            if (Android.OS.Build.VERSION.SdkInt < BuildVersionCodes.R)
            {
                View tView = _instance.View;
                if (!string.IsNullOrEmpty(backgroundHexColor))
                {
                    tView.Background.SetColorFilter(Color.ParseColor(backgroundHexColor),
                                                    PorterDuff.Mode
                                                    .SrcIn); //Gets the actual oval background of the Toast then sets the color filter
                }
                TextView text = (TextView)tView.FindViewById(Android.Resource.Id.Message);
                if (!string.IsNullOrEmpty(textHexColor))
                {
                    text.SetTextColor(Color.ParseColor(textHexColor));
                }
            }
            _instance.Show();
        }
Exemplo n.º 2
0
        /// <summary>
        /// ShowToastMessage
        /// </summary>
        /// <param name="message"></param>
        /// <param name="backgroundHexColor"></param>
        public void ShowToastMessage(string message, string backgroundHexColor = null, string textHexColor = null)
        {
            // To dismiss existing toast, otherwise, the screen will be populated with it if the user do so
            _instance?.Cancel();
            _instance = Android.Widget.Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);
            View tView = _instance.View;

            if (!string.IsNullOrEmpty(backgroundHexColor))
            {
                tView.Background.SetColorFilter(Color.ParseColor(backgroundHexColor), PorterDuff.Mode.SrcIn);//Gets the actual oval background of the Toast then sets the color filter
            }
            TextView text = (TextView)tView.FindViewById(Android.Resource.Id.Message);

            if (!string.IsNullOrEmpty(textHexColor))
            {
                text.SetTextColor(Color.ParseColor(textHexColor));
            }
            _instance.Show();
        }
Exemplo n.º 3
0
        private void ShowToastMessage(string message)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    _instance?.Cancel();
                    _instance = Android.Widget.Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);

                    TextView textView;
                    Snackbar snackBar = null;

                    var tView = _instance.View;
                    if (tView == null)
                    {
                        // Since Android 11, custom toast is deprecated - using snackbar instead:

                        Activity activity = CrossCurrentActivity.Current.Activity;
                        var view          = activity.FindViewById(Android.Resource.Id.Content);

                        snackBar = Snackbar.Make(view, message, Snackbar.LengthLong);

                        textView = snackBar.View.FindViewById <TextView>(Resource.Id.snackbar_text);
                    }
                    else
                    {
                        // using Toast

                        tView.Background.SetColorFilter(Android.Graphics.Color.Gray, PorterDuff.Mode.SrcIn); //Gets the actual oval background of the Toast then sets the color filter
                        textView = (TextView)tView.FindViewById(Android.Resource.Id.Message);
                        textView.SetTypeface(Typeface.DefaultBold, TypefaceStyle.Bold);
                    }

                    var minTextSize = textView.TextSize; // 16

                    textView.SetTextColor(Android.Graphics.Color.White);

                    var screenHeightRate = 0;

                    //configuration font size:

                    //Normal = 0,
                    //AboveNormal = 1,
                    //Big = 2,
                    //Biger = 3,
                    //VeryBig = 4,
                    //Huge = 5

                    if (DeviceDisplay.MainDisplayInfo.Height < DeviceDisplay.MainDisplayInfo.Width)
                    {
                        // Landscape

                        screenHeightRate = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Height / 16.0);
                        textView.SetMaxLines(2);
                    }
                    else
                    {
                        // Portrait

                        screenHeightRate = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Height / 32.0);
                        textView.SetMaxLines(4);
                    }

                    var fontSizeRange    = screenHeightRate - minTextSize;
                    var fontSizePerValue = fontSizeRange / 5;

                    var fontSize = minTextSize + (int)_cfg.AppFontSize * fontSizePerValue;

                    textView.SetTextSize(Android.Util.ComplexUnitType.Px, Convert.ToSingle(fontSize));

                    if (snackBar != null)
                    {
                        snackBar.Show();
                    }
                    else
                    {
                        _instance.Show();
                    }
                }
                catch (Exception ex)
                {
                    _loggingService.Error(ex);
                }
            });
        }
Exemplo n.º 4
0
 /// <summary>
 /// ShowToastMessage
 /// </summary>
 /// <param name="message"></param>
 public void ShowToastMessage(string message)
 {
     Android.Widget.Toast Task = Android.Widget.Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);
     Task.Show();
 }
Exemplo n.º 5
0
 static void SetTextAndShow(AndroidToast t, string text)
 {
     // 某些定制ROM会更改内容文字,例如MIUI,重新设置可强行指定文本
     t.SetText(text);
     t.Show();
 }
Exemplo n.º 6
0
        public void Show(ToastView view, object viewModel = null)
        {
            view.Parent         = XF.Application.Current.MainPage;
            view.BindingContext = viewModel;

            var toast = new Android.Widget.Toast(Dialogs.Context);

            var offsetX = (int)Dialogs.Context.ToPixels(view.OffsetX);
            var offsetY = (int)Dialogs.Context.ToPixels(view.OffsetY);

            // HACK: For some reason, the offset direction is reversed when GravityFlags contains Left or Bottom.
            if (view.HorizontalLayoutAlignment == XF.LayoutAlignment.End)
            {
                offsetX *= -1;
            }
            if (view.VerticalLayoutAlignment == XF.LayoutAlignment.End)
            {
                offsetY *= -1;
            }

            toast.SetGravity(Dialogs.GetGravity(view), offsetX, offsetY);
            toast.Duration = Android.Widget.ToastLength.Long;

            var renderer = Dialogs.CreateNativeView(view);

            var measure = Dialogs.Measure(view);

            view.Layout(new XF.Rectangle(new XF.Point(0, 0), measure));

            var realW = (int)Dialogs.Context.ToPixels(measure.Width);
            var realH = (int)Dialogs.Context.ToPixels(measure.Height);

            var layout = new LinearLayout(Dialogs.Context);

            using (var param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
            {
                Width = realW,
                Height = realH
            }){
                layout.LayoutParameters = param;
            }

            using (var param = new LinearLayout.LayoutParams(realW, realH)
            {
                Width = realW, Height = realH
            }){
                layout.AddView(renderer.View, param);
            }

            if (view.CornerRadius > 0)
            {
                var border = new GradientDrawable();
                border.SetCornerRadius(Dialogs.Context.ToPixels(view.CornerRadius));
                if (!view.BackgroundColor.IsDefault)
                {
                    border.SetColor(view.BackgroundColor.ToAndroid());
                    border.Alpha = (int)(view.Opacity * 255);
                }
                layout.ClipToOutline = true;
                layout.SetBackground(border);
            }

            toast.View = layout;

            view.RunPresentationAnimation();

            toast.Show();

            var duration = Math.Max(Math.Min(view.Duration - 260, 3500), 0); // give a bit millisecond margin

            var handler = new Handler();

            handler.PostDelayed(new Runnable(view.RunDismissalAnimation), duration);

            handler.PostDelayed(new Runnable(() =>
            {
                //view.RunDismissalAnimation();
                //await Task.Delay(250);
                toast?.Cancel();

                view.Parent = null;

                if (!renderer.View.IsDisposed())
                {
                    renderer.View.RemoveFromParent();
                    renderer.View.Dispose();
                }

                layout.Dispose();

                // I coudn't reproduce https://github.com/muak/AiForms.Dialogs/issues/2.
                // But I let this code disabled because it has no influent even if it is disabled.
                //toast.View = null;

                renderer.Dispose();
                renderer = null;
                toast?.Dispose();
                toast = null;

                view.Destroy();
                view.BindingContext = null;
            }), view.Duration);
        }