Create StopWatch Application In Xamarin.Android Using Visual Studio

Xamarin Introduction

Xamarin is the best cross platform tool to develop mobile applications. It provides cross platform app development in C#, so we don’t need to write in Java or Objective C. We can just use C# and leverage the benefits on all the platforms. Xamarin also helps designers by providing with the different platforms like Android, iOS, etc.

Working with Xamarin Application Development, we have two gateways- Xamarin Studio and Visual Studio. Xamarin Studio supports both, Windows or Mac. Visual Studio supports only Windows, provided, you can build and debug the application on Mac.

Prerequisites

  • Visual Studio 2017 RC.

The following steps are required to be followed in order to create StopWatch application in Xamarin.Forms using VS 2017.

Xamarin

Step 1

Go to the Visual Studio, File >> New >> Project.

Click on C# >> Android >> Blank App(Android).

Enter the application name and click OK.

Now, you can see the homepage of the project.

Step 2

In this step, we are going create 3 buttons and labels for displaying timer.

Xamarin

Xamarin

Xamarin

Step 3

In this step, give the code for Start button click event and Pause button click event in MainActivity.cs.

Xamarin

Xamarin

  1. btnStart.Click += delegate {  
  2.     timer = new Timer();  
  3.     timer.Interval = 1; // 1 milliseconds  
  4.     timer.Elapsed += Timer_Elapsed;  
  5.     timer.Start();  
  6. };  
  7. btnPasuse.Click += delegate {  
  8.     timer.Stop();  
  9.     timer = null;  
  10. };  

Step 4

In this step, assign the Timer_Elapsed to timer label.

Xamarin

Step 5

In this step, write code for Lap button click event.

  1. btnLap.Click += delegate {  
  2.     LayoutInflater inflater = (LayoutInflater) BaseContext.GetSystemService(Context.LayoutInflaterService);  
  3.     View addView = inflater.Inflate(Resource.Layout.row, null);  
  4.     TextView txtContent = addView.FindViewById < TextView > (Resource.Id.textView1);  
  5.     txtContent.Text = txtTimer.Text;  
  6.     container.AddView(addView);  
  7. };  

Xamarin

Step 6

Run the app.

Xamarin

Xamarin

Xamarin

I hope this article is very useful.


Similar Articles