Creating A ProgressDialog In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Visual Studio, the Xamarin Studio is also available.

Prerequisites
  • Visual Studio 2015 Update 3
The steps, given below, are required to be followed in order to create a ProgressDialog in the Xamarin Android app, using Visual Studio 2015.
 
Step 1
 
Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio. Or press "Ctrl+Shift+N".
 
 
 
Step 2
 
After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).
 
Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.
 
 
 
Step 3

Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page. If you select Source mode, you can write the xaml code. If you select the Designer mode, you can design your app.
 
 
 
Step 4
 
Opening the main.axml file will open the main page designer. You can design the page as per your expertise, selecting any mode- Source or Designer.
 
 
 
Next, delete the default "hello world" button from the source panel by deleting the button code. Also, delete the C# button action code from the MainActivity.cs page.
 
Step 5
 
Now, go to the toolbox window. You need to scroll down and you will see all the tools and controls. Drag and drop a Button.  
 
 
 
Step 6 
 
Now, go to the Properties window. You need to edit the Button Id Value(EX: android:id="@+id/editText").
 
 
 
Step 7
 
In this step, go to the Main.axml page Source Panel and note down the Button id value.
 
Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     
  3.    android:orientation="vertical"      
  4.    android:layout_width="match_parent"      
  5.    android:layout_height="match_parent"     
  6.    android:minWidth="25px"      
  7.    android:minHeight="25px">    
  8.     
  9. <Button    
  10.     
  11.    android:text="Check"      
  12.    android:layout_width="match_parent"      
  13.    android:layout_height="wrap_content"     
  14.    android:id="@+id/MyButton" />    
  15.   
  16. </LinearLayout>
 
 
Step 8
 
In this step, go to the MainActivity.cs page in Solution Explorer and write the following code in OnCreate() Method.
 
MainActivity.cs
  1. namespace Progress_dialog  
  2. {  
  3.     [Activity(Label = "Progress_dialog", MainLauncher = true, Icon = "@drawable/icon")]  
  4.     public class MainActivity: Activity  
  5.     {  
  6.         Android.App.ProgressDialog progress;  
  7.         protected override void OnCreate(Bundle bundle)  
  8.         {  
  9.             base.OnCreate(bundle);  
  10.             // Set our view from the "main" layout resource  
  11.             SetContentView(Resource.Layout.Main);  
  12.             Button button = FindViewById < Button > (Resource.Id.MyButton);  
  13.             button.Click += delegate  
  14.             {  
  15.                 progress = new Android.App.ProgressDialog(this);  
  16.                 progress.Indeterminate = true;  
  17.                 progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);  
  18.                 progress.SetMessage("Loading is Progress...");  
  19.                 progress.SetCancelable(false);  
  20.                 progress.Show();  
  21.             };  
  22.         }  
  23.     }  

 
 
Step 9
 
If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.
 
Simply, connect your phone and go to Visual Studio.
 
The connected phone will show up in the Run menu (Ex: LENOVO A6020a40 (Android 5.1-API 22)). Click the Run option.
 
 

Output
 
After a few seconds, the app will start running on your phone. You will see your app running successfully. Now, click "Check".
 
 
You will see that the ProgressDialog is working successfully.
 
 

Summary

So, this was the process of how to create a ProgressDialog in Xamarin Android app, using Visual Studio 2015

Up Next
    Ebook Download
    View all
    Learn
    View all