Search code examples
c#androidxamarinxamarin.androidandroid-runonuithread

How to run code after a delay in Xamarin Android


I'm trying to show some code after a delay in my Android app.
The Java code for doing this is something like this:

new Handler().postDelayed(new Runnable()
{
   @Override
   public void run()
   {
     // your code that you want to delay here
   }
}, 1000/* 1000ms = 1sec delay */);

How do I do this in Xamarin.Android with C#?


Solution

  • You can try this:

    Handler h = new Handler();
    Action myAction = () => 
    {
        // your code that you want to delay here
    };
    
    h.PostDelayed(myAction, 1000);
    

    Take a look at document