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

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...

Answers

  • StevenTheEvenStevenTheEven SEMember ✭✭✭

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

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭
    edited August 2013

    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?

  • StevenTheEvenStevenTheEven SEMember ✭✭✭
    edited August 2013

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

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭
    edited August 2013

    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

  • VijayPawar.0297VijayPawar.0297 USMember ✭✭

    @AlvinGeorge.Paruvanany said:
    On Android:

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

    its not working for me.

  • VijayPawar.0297VijayPawar.0297 USMember ✭✭

    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

  • RyanFrenchRyanFrench USMember ✭✭

    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);
                }
    
  • DooksDooks ZAMember ✭✭✭

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

  • HarshitaHarshita INMember ✭✭✭✭

    Make Toast Message in Xamarin Forms,Here is full guide:

  • AhmedAbubakarAhmedAbubakar USMember ✭✭

    @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

  • VWilcox2000VWilcox2000 Member ✭✭

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

Sign In or Register to comment.