locked
Alert message using Toast.MakeText not showing resolved as Unknown identifier RRS feed

  • Question

  • User20002 posted

    I am calling below function inside thread, but its not showing the alert message?

        protected void RemindCurrentTime()
                {
                    DateTime now = DateTime.Now;
                    string currentTime = (string.Format ("Current Time: {0}", now));
                    Toast.MakeText (this, currentTime, ToastLength.Long).Show();
                    }
    

    While running i set break-point, it executing that line, but resolving as Unknown identifier: Toast. Please someone help me...

    Wednesday, August 21, 2013 5:59 AM

All replies

  • User8505 posted

    http://docs.xamarin.com/guides/android/advancedtopics/writingresponsive_applications
    RunOnUiThread (() => textview.Text = "Method Complete");

    Wednesday, August 21, 2013 7:35 AM
  • User20002 posted

    Hi Michael,

    Thanks for your reply. When i use RunOnUiThread in my code, i am getting error, that

    RunOnUiThread doesn't exist in current context

    As documentaion says

    However, unlike Activities, services don’t have a RunOnUIThread method. From services, a Handler can be used to achieve the same result.

    Hence i think i can't use that but i am not sure. I want Notification alert from services. Hence i tried Handler method also, but no use

    myHandler.Post(() => {
                    Toast.MakeText (this._context, currentTime, ToastLength.Long).Show();
                });
    

    Is there any other way?

    Wednesday, August 21, 2013 8:28 AM
  • User8505 posted

    Have you tried http://docs.xamarin.com/guides/android/applicationfundamentals/services/part1-started_services 4.1. Using Notifications ?
    Do you have using Android.Widget;

    Wednesday, August 21, 2013 8:40 AM
  • User20002 posted

    Ya using Notification earlier i did as below

        var nMgr = (NotificationManager)GetSystemService (NotificationService);
                var notification = new Notification (Resource.Drawable.iconBlue, currentTime);
                var pendingIntent = PendingIntent.GetActivity (this, 0, new Intent (this, typeof(HomeScreen)), 0);
                notification.SetLatestEventInfo (this, "Reminder Service Notification", currentTime, pendingIntent);
                nMgr.Notify (0, notification);
    

    Above thing is working perfect. But my requirement is, i need to show like alert message, not notification. i am using Android Widget in my reference

    Wednesday, August 21, 2013 8:51 AM
  • User191013 posted

    On Android:

    Toast.MakeText (this, "click", ToastLength.Long).Show ();

    Tuesday, February 23, 2016 9:43 AM
  • User275516 posted

    @AlvinGeorge.Paruvanany said: On Android:

    Toast.MakeText (this, "click", ToastLength.Long).Show ();

    its not working for me.

    Thursday, December 1, 2016 5:56 AM
  • User275516 posted

    I found the reason behind my project problem. The Visual Studio Emulator does not clear the earlier deployed app/project. For this I tried first close and restart VS Emulator then it worked. But every time it is not possible. Now thats new problem

    Thursday, December 1, 2016 7:12 AM
  • User243296 posted

    This is how you get a toast from a background thread:

         public void ShowToast(string text, bool IsLengthShort = false)
                {
                    Handler mainHandler = new Handler(Looper.MainLooper);
                    Java.Lang.Runnable runnableToast = new Java.Lang.Runnable(() =>
                    {
                        var duration = IsLengthShort ? ToastLength.Short : ToastLength.Long;
                        Toast.MakeText(Forms.Context, text, duration).Show();
                    });
    
                    mainHandler.Post(runnableToast);
                }
    
    Thursday, October 19, 2017 9:26 PM
  • User237535 posted

    @RyanFrench I will kiss you on the mouth. Thank you!

    Friday, October 20, 2017 11:57 AM
  • User355485 posted

    Make Toast Message in Xamarin Forms,Here is full guide: https://youtu.be/QXEW79CkkqM

    Friday, January 12, 2018 4:14 PM
  • User327165 posted

    @RyanFrench said: This is how you get a toast from a background thread:

         public void ShowToast(string text, bool IsLengthShort = false)
                {
                    Handler mainHandler = new Handler(Looper.MainLooper);
                    Java.Lang.Runnable runnableToast = new Java.Lang.Runnable(() =>
                    {
                        var duration = IsLengthShort ? ToastLength.Short : ToastLength.Long;
                        Toast.MakeText(Forms.Context, text, duration).Show();
                    });
    
                    mainHandler.Post(runnableToast);
                }
    

    This saved my day!! thanks

    Friday, March 8, 2019 10:07 AM
  • User386324 posted

    @RyanFrench thanks! I won't kiss you on the mouth for which you are welcome as well! ;)

    Friday, June 7, 2019 3:50 PM