0

I want AlertDialog with message "1" to appear on screen and after 3 seconds to dissappear then I want another AlertDialog with message "2" to appear on screen and after 3 seconds to dissappear and so on and so forth until 5. I have the following code:

[Activity(Label = "TestAlertDialogBuilder", MainLauncher = true)]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        for (int i=0; i <=5 ; i++)
        {
            //making the program stop so I can see the alert showing and dissappearing
            System.Threading.Thread.Sleep(3000);
            ShowAlert(i);
        }
    }

    Dialog dialog;
    public void ShowAlert(int i)
    {
        if (dialog != null)
        {
            dialog.Cancel();
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.SetMessage(i.ToString());
        dialog = alertDialog.Create();
        dialog.Show();
    }
}

But after the program's execution after some waiting I only get an AlertDialog with message "5" in it and that is. If someone think another title for my question is more appropriate he can change it.

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24

2 Answers2

1

Just convert Mohamed Mohaideen AH's answer to C# language.

public class MyRunnable : Java.Lang.Object, Java.Lang.IRunnable
{
    private MainActivity mainActivity;

    public MyRunnable(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    public void Run()
    {
        for (int i = 0; i <= 5; i++)
        {
            try
            {
                Java.Lang.Thread.Sleep(3000);
            }
            catch (Java.Lang.Exception e)
            {
                e.PrintStackTrace();
            }
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ShowAlert(i);
            });
        }
    }
}


[Activity(Label = "App5", MainLauncher = true)]
public class MainActivity : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);


        MyRunnable myRunnable = new MyRunnable(this);
        Java.Lang.Thread thread = new Java.Lang.Thread(myRunnable);
        thread.Start();

    }
    Dialog dialog;
    public void ShowAlert(int i)
    {
        if (dialog != null)
        {
            dialog.Cancel();
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.SetMessage("AlertDialog: " + i);
        dialog = alertDialog.Create();
        dialog.Show();
    }
}
ColeX
  • 14,062
  • 5
  • 43
  • 240
0

Issue happened due to blocking UI Main thread until for loop executed. So create seperate thread for dialog & show it in UI.

Try this

Thread thread = new Thread(new Runnable() {

        int i = 0;
        @Override
        public void run() {
            for (i=0; i <=5 ; i++)
            {
                //making the program stop so I can see the alert showing and dissappearing
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ShowAlert(i);
                    }
                });

            }

        }
    });
   thread.start();

 public void ShowAlert(int i)
{
    if (dialog != null)
    {
        dialog.cancel();
    }
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    alertDialog.setMessage("" + i);
    dialog = alertDialog.create();
    dialog.show();
}
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24