Forum Xamarin.Android

How to make Toast appear

RonNYCRonNYC Member ✭✭✭
edited September 2019 in Xamarin.Android

I have a procedure to run Toast:

 protected void MakeToast(string toastText)

            var toast = Toast.MakeText(this, toastText, ToastLength.Long); 
            toast.SetGravity(GravityFlags.Center, 0, 0); 
            toast.View.SetPadding(10, 10, 10, 10); 
            var toastView = (ViewGroup)toast.View; 
            if (toastView.ChildCount > 0 && toastView.GetChildAt(0) is TextView)
                TextView tv = (TextView)toastView.GetChildAt(0);
                tv.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);

                tv.SetTextSize(Android.Util.ComplexUnitType.Sp, 15);

And it usually works...but...when it precedes a long-running and CPU intensive procedure, it often doesn't show. Or it shows when the procedure has run and then it shows "Working" and immediately, "Completed". Since the app is unresponsive when this long running procedure starts, I really want the user to know pressing the button works. Is there anything I can do about this?



  • suraj007suraj007 Member ✭✭✭

    try this code........
    await Task.Run(() =>
    this.RunOnUiThread(() =>
    MakeToast("toastText") //your toast method call

  • ColeXColeX Member, Xamarin Team Xamurai
    edited September 2019

    Where do you call the method MakeToast ? In background thread or a asynchronous task ?

    If in background thread , try the workaround .

    If in asynchronous task , wrap the method into RunOnUiThread like @suraj007 said.

  • suraj007suraj007 Member ✭✭✭

    make a asynchronous task.

  • RonNYCRonNYC Member ✭✭✭
    edited September 2019

    This is what I have:

                    RunOnUiThread(() => {

    It directly precedes the initiation of the long-running task and Working does not appear. It will appear if the longrunnign task doesn't run very long. The task is dependant on the data submitted to it.

  • RonNYCRonNYC Member ✭✭✭
    edited September 2019

    What also happens is "Working" often comes after "Completed". I know I'm making some kind of error, just can't figure out what. The user pushes a button which, after some tests, initiates a long running task. Before the task starts I use toast to announce to the user the work has begun. I wouldn't mind the button changing shape or the button text changing. I tried that but it also didn't work.

    The task initiation is from MainActivity.

    Is there another way to alert the user that the button push started something?

    I disable the button after it is pushed.

    But I tried this kind of function:

    the btnEmail initiates the sendEmail( ) procedure.

    In this procedure, I go:


    but the button can still be pushed.


Sign In or Register to comment.