1

I have created the following alert dialog and progressbar:

LayoutInflater layoutInflater = LayoutInflater.From(this);
View progressDialogBox = layoutInflater.Inflate(Resource.Layout.progress_dialog_box, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.SetView(progressDialogBox);
var progressBar1 = progressDialogBox.FindViewById<ProgressBar>(Resource.Id.progressBar1);
progressBar1.Max = 100;
progressBar1.Progress = 0;

Dialog dialog = alertDialogBuilder.Create();
dialog.Show();

And I make it increase every 2 seconds

System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);

progress_dialog_box.axml contains that code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Getting data..."
        android:layout_gravity="center"
        android:paddingTop="40dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar1" />
</LinearLayout>

But when I run the application the ProgressBar stays at 0. How can I make it progress?

Profile2ForStack
  • 473
  • 2
  • 8
  • 16

1 Answers1

2

But when I run the application the ProgressBar stays at 0.

It is 100 instead of 0. progressBar1.IncrementProgressBy(25); will be called before the dialog shows(I haven't found why the process have been done before the dialog shows, I will update my solution if I found).

Here is a simple:

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);

        LayoutInflater layoutInflater = LayoutInflater.From(this);
        View progressDialogBox = layoutInflater.Inflate(Resource.Layout.progress_dialog_box, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.SetView(progressDialogBox);
        var progressBar1 = progressDialogBox.FindViewById<ProgressBar>(Resource.Id.progressBar1);
        progressBar1.Max = 100;
        progressBar1.Progress = 0;

        Dialog dialog = alertDialogBuilder.Create();
        dialog.Show();


        UpdatePB uptask = new UpdatePB(this, progressBar1);
        uptask.Execute(100);
    }

    public class UpdatePB : AsyncTask<int, int, string>
    {
        Activity mcontext;
        ProgressBar mpb;
        public UpdatePB(Activity context, ProgressBar pb)
        {
            this.mcontext = context;
            this.mpb = pb;
        }
        protected override string RunInBackground(params int[] @params)
        {
            // TODO Auto-generated method stub
            for (int i = 1; i <= 4; i++)
            {
                try
                {
                    System.Threading.Thread.Sleep(3000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    Android.Util.Log.Error("lv", e.Message);
                }
                mpb.IncrementProgressBy(25);
                PublishProgress(i * 25);

            }
            return "finish";
        }

        protected override void OnProgressUpdate(params int[] values)
        {
            Android.Util.Log.Error("lv==", values[0] + "");
        }
        protected override void OnPostExecute(string result)
        {
            mcontext.Title = result;
        }


    }
} 

And change your ProgressBar's width to match_parent, it will be better.

Robbit
  • 4,300
  • 1
  • 13
  • 29