16

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#?

amitairos
  • 2,907
  • 11
  • 50
  • 84

2 Answers2

31

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

ductran
  • 10,043
  • 19
  • 82
  • 165
1

I advise you to use cross-platform timer like AdvancedTimer. Check:github repo

API Usage

To gain access to the Timer class simply use the dependency service:

IAdvancedTimer timer = DependencyService.Get<IAdvancedTimer>();

You MUST call initTimer for timer initialization;

timer.initTimer(3000, timerElapsed, true);

initTimer(interval, Eventhandler function, AutoReset);

Methods

timer.startTimer();

timer.stopTimer();

timer.getInterval()

timer.setInterval(5000);

timer.isTimerEnabled();
Andrew Carl
  • 802
  • 1
  • 8
  • 15
  • Thanks. Do you know how I can implement this in this instance: http://stackoverflow.com/questions/39124434/animate-recyclerview-items-one-by-one ? – amitairos Aug 28 '16 at 09:31