Run And Stop Activity Indicator In Xamarin Forms

Introduction
 
This article demonstrates the Run and Stop activity indicator in Xamarin.Forms application.
 
Let's start.
 
Step 1
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-Platform app, then give your project a name and location.
 
 
 
Step 2 
 
 Update the following NuGet Packages to your project.
  • Xamarin.Forms 

     
Now, select the following NuGet Packages and select your project to update the packages.   
 
 
 
Step 3
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
Xaml Code 
 
We are creating an activity indicator in AbsoluteLayout and a button inside the StackLayout. Two buttons are created -- one button in "Run" and the second button in "Stop". 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Activity_Indicator"  
  5.              x:Class="Activity_Indicator.MainPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
  8.             <AbsoluteLayout>  
  9.                 <ActivityIndicator x:Name="activity"  
  10.                        IsRunning="False"  
  11.                        IsEnabled="False"  
  12.                        IsVisible="False"  
  13.                        Color="Blue"  
  14.                        BackgroundColor="Transparent"/>  
  15.             </AbsoluteLayout>  
  16.             <Button Text="Run" Clicked="Button_Clicked"/>  
  17.             <Button Text="Stop" Clicked="Button_Clicked_1"/>  
  18.         </StackLayout>  
  19.     </ContentPage.Content>  
  20. </ContentPage>  
Step 4 
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml.cs. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
C# Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace Activity_Indicator  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.         private void Button_Clicked(object sender, EventArgs e)  
  18.         {  
  19.             activity.IsEnabled = true;  
  20.             activity.IsRunning = true;  
  21.             activity.IsVisible = true;  
  22.         }  
  23.   
  24.         private void Button_Clicked_1(object sender, EventArgs e)  
  25.         {  
  26.             activity.IsEnabled = false;  
  27.             activity.IsRunning = false;  
  28.             activity.IsVisible = false;  
  29.         }  
  30.     }  
  31. }  
Step 5 
 
Next, select the build & deploy option, followed by selecting from the list of Android Emulator or Simulator. You can choose any API (Android Program Interface) Level Emulator or Simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Output 1 
 
Click the "Run" Button and run the activity indicator.
 
 
 
Output 2 
 
Click the "Stop" Button to stop the activity indicator.
 
 
 
Output 3
  
The result is displayed.
 
 
 
Finally, we have successfully created a Xamarin.Forms Run and Stop Activity Indicator. 


Similar Articles