Share via


How to run code after a delay in Xamarin Android

Question

Sunday, August 28, 2016 7:40 AM

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

All replies (5)

Sunday, August 28, 2016 3:48 PM

I think using Thread.Sleep(1000); will help you.


Sunday, August 28, 2016 9:22 PM

Use async/await and await Task.Delay(TimeSpan.FromSeconds(1))


Tuesday, January 30, 2018 3:55 AM

I'm trying to do something similar, but what I want to happen is to delay the autocomplete drop down so it's not trying to update after each TextChanged event. So in AfterTextChanged, I create a timer, set the timer's Elapsed event to a delegate that creates an ArrayAdapter and sets it to the autocomplete's Adapter. And in TextChanged, I check to see if that timer is null, and if it isn't, I close it so it doesn't fire its elapsed event. But it seems like the autocomplete's suggestion box never gets updated.

    using System.Timers;
        foodEntry.TextChanged += delegate
        {
            if (searchTimer != null)
                searchTimer.Close();
        };

        foodEntry.AfterTextChanged += delegate
        {
            searchTimer = new Timer(600);
            searchTimer.Elapsed += delegate
            {
                foodEntry.Adapter = new ArrayAdapter(this, 
                                        Android.Resource.Layout.SimpleDropDownItem1Line,
                                        ParseSearch(SearchUSDA(foodEntry.Text)).ToArray());
            };
        };

This was kind of a modification of an example I saw that used Java. (I can't post a link, but it was from Future Studio "How to delay TextChangedEvent on Androids EditText.")


Saturday, May 19, 2018 1:05 PM

@Teravian - I assume you've figured out what was wrong by now [you never call searchTime.Start()];
I just wanted to mention for anyone else trying to do similar: There is no benefit to using both TextChanged and AfterTextChanged for what you are trying to do; the two methods happen one right after the other, so you might as well put all the code into one method.

And a minor note on efficiency: rather than destroy and create a new timer each time, just Stop the existing one.
Putting it all together:

using System.Timers;
Timer searchTimer;
foodEntry.TextChanged += delegate
{
    if (searchTimer != null)
        searchTimer.Stop();
    else
    {
        searchTimer = new Timer(600);
        searchTimer.Elapsed += delegate
        {
            foodEntry.Adapter = new ArrayAdapter(this, 
                                    Android.Resource.Layout.SimpleDropDownItem1Line,
                                    ParseSearch(SearchUSDA(foodEntry.Text)).ToArray());
        };
    }
    // Start the time interval. ("Stop" above makes sure we are starting a new, full, interval.)
    searchTimer.Start();
};

Then in whatever method you have that is called when user is done editing or the control loses focus:

    if (searchTimer != null)
    {
        searchTimer.Close();
        searchTimer = null;
    }

Or in c# 6.0+ (null conditional operator):

    searchTimer?.Close();
    searchTimer = null;

Thursday, May 9, 2019 9:45 AM

    new Handler().PostDelayed(delegate
                {
                    // Your code here
                }, delayInMilliseconds);