Esempio 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();
        }
Esempio n. 2
0
        protected void ShowToast(string message, ToastLength length = ToastLength.Long)
        {
            var activity = Activity;

            if (activity == null)
            {
                return;
            }

            AToast.MakeText(activity, message, length).Show();
        }
    /// <summary>
    ///     Presents an toast based on the provided configuration.
    /// </summary>
    /// <param name="config">The dialog configuration.</param>
    protected override void PresentToast(IToastConfig config)
    {
        if (MessagingServiceCore.ActivityLifecycleCallbacks.CurrentActivity is AppCompatActivity appCompatActivity)
        {
            appCompatActivity.SafeRunOnUIThread(() =>
            {
                var toast = Android_Toast.MakeText(MessagingServiceCore.ActivityLifecycleCallbacks.CurrentActivity, config.Message, config.Duration);

                // If a layout resource ID has been provided, create the view with the view manager.
                if (config.LayoutResID.HasValue)
                {
                    // Inflate the custom Toast view and configure any subviews.
                    toast.View = MessagingServiceCore.ViewManager.InflateView(config.LayoutResID.Value, null, false, (view, dialogElement, autoHide) =>
                    {
                        switch (view, dialogElement)
                        {
        void IToastPlatformService.Show(string text, int duration)
        {
            try
            {
                if (MainThread.IsMainThread)
                {
                    Show_();
                }
                else
                {
                    MainThread.BeginInvokeOnMainThread(Show_);
                }

                void Show_()
                {
                    var context   = Application.Context;
                    var duration2 = (ToastLength)duration;

                    // https://blog.csdn.net/android157/article/details/80267737
                    try
                    {
                        if (toast == null)
                        {
                            toast = AndroidToast.MakeText(context, text, duration2);
                            if (toast == null)
                            {
                                throw new NullReferenceException("toast markeText Fail");
                            }
                        }
                        else
                        {
                            toast.Duration = duration2;
                        }
                        SetTextAndShow(toast, text);
                    }
                    catch (Exception e)
                    {
                        Log.Error(TAG, $"text: {text}{Environment.NewLine}{e}");
                        // 解决在子线程中调用Toast的异常情况处理
                        Looper.Prepare();
                        var _toast = AndroidToast.MakeText(context, text, duration2)
                                     ?? throw new NullReferenceException("toast markeText Fail(2)");
                        SetTextAndShow(_toast, text);
                        Looper.Loop();
                    }
Esempio n. 5
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();
        }
Esempio n. 6
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);
                }
            });
        }
Esempio n. 7
0
 public void ShowToast(string message) =>
 Device.InvokeOnMainThreadAsync(() =>
                                AToast.MakeText(Application.Context, message, ToastLength.Short).Show());
Esempio n. 8
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();
 }
Esempio n. 9
0
 public void Show()
 {
     AndroidToast.MakeText(MainActivity, this.Text, ToastLength.Short).Show();
 }
Esempio n. 10
0
 private void ToastMessage(string mssg)
 => Toast.MakeText(Activity, mssg, ToastLength.Long).Show();
Esempio n. 11
0
 static void SetTextAndShow(AndroidToast t, string text)
 {
     // 某些定制ROM会更改内容文字,例如MIUI,重新设置可强行指定文本
     t.SetText(text);
     t.Show();
 }
        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);
        }
Esempio n. 13
0
 protected void ShowToast(string message, ToastLength length = ToastLength.Long)
 {
     AToast.MakeText(this, message, length).Show();
 }