pypypu
pypypu

Reputation: 155

Xamarin - ActivityIndicator not valid

I try to add an ActivityIndicator in Android app. When can add the code in xml, or directly in code page. But in the code, I can create new indicator with xamarin forms, but I don't know how to add the indicator into linear layout.

And if I add indicator into a axml file, it tells me that the ActivityIndicator is not valid.

In page:

 linearLayout1 = FindViewById<LinearLayout>(Resource.Id.linearLayout1);
 var indicator = new Xamarin.Forms.ActivityIndicator();
 linearLayout1.AddView(indicator); //error, indicator not is a view

xml:

 <ActivityIndicator
            Color="Red"
            IsRunning="true" />

Upvotes: 0

Views: 284

Answers (2)

Kiefer
Kiefer

Reputation: 13

I had some issues with getting mine to show. I am also using the native android ProgressDialog which is very simple to implement.

I used MainActivity and passed that into the ProgressDialog constructor. Seemed to work for me. Also wrapping everything inside a Device.BeginInvokeOnMainThread fixed it for me while working on both android and iOS. I think it was because the overlay has to be instantiated on the main UI thread.

Device.BeginInvokeOnMainThread(() =>
        {
            MainActivity activity = Forms.Context as MainActivity;
            progress = new ProgressDialog(activity);
            progress.Indeterminate = true;
            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
            progress.SetMessage("Connecting to server.. Please wait..");
            progress.SetCancelable(false);
            progress.Show();
        });

Not sure if this is exactly what you need, but it may help you solve your problem!

Upvotes: 0

Alessandro Caliaro
Alessandro Caliaro

Reputation: 5768

If you are using Xamarin.Android to create your app you should not be using an ActivityIndicator, which is a Xamarin.Forms control, but instead use a ProgressBar widget.

From Xamarin doc:

The following code example shows how a progress bar can be used from a worker thread to update the user interface to notify the user of progress:

 public class MyActivity extends Activity {
     private static final int PROGRESS = 0x1;

     private ProgressBar mProgress;
     private int mProgressStatus = 0;

     private Handler mHandler = new Handler();

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.progressbar_activity);

         mProgress = (ProgressBar) findViewById(R.id.progress_bar);

         // Start lengthy operation in a background thread
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     mProgressStatus = doWork();

                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
     }
 }

To add a progress bar to a layout file, you can use the element. By default, the progress bar is a spinning wheel (an indeterminate indicator)

Upvotes: 1

Related Questions