Download Code

In this tutorial you will learn how to create number Counter application using Threads with Android Runnable,Thread Handler class.

I have discussed about Threads , Android Runnable(Link) and Thread Handler class in Previous Tutorials.So we will start directly.

Thread Handler

Discussed in previous tutorial

Android Runnable

Android_Runnable.

Now create new Android Application Project.We will use a text view and a button in Main.axml.

android runnable,hanlder,thread handler,xamarin,android xamarin,C# xamarin,runnable,run

Now go to your MainActivity.cs class.Create variable’s for textview and button.Refer their id’s.Then implements click method.

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using System;
using Android.Content;
using Android.Views;

namespace XamarinCounterApp
{
	[Activity(Label = "XamarinCounterApp", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		Button resetBtn;
		TextView countTxt;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			countTxt = ( TextView )FindViewById(Resource.Id.counter);
			resetBtn = ( Button )FindViewById(Resource.Id.reBtn);
			resetBtn.Click += ResetBtn_Click;

		}
		void ResetBtn_Click(object sender, System.EventArgs e)
		{
			
		}
	}
}

Next Step create variable for delay,elapsedTime,Handler.Then create Runnable variable and Run,count number method.
MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using System;
using Android.Content;
using Android.Views;

namespace XamarinCounterApp
{
	[Activity(Label = "XamarinCounterApp", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{

		Button resetBtn;
		TextView countTxt;
		int elapsedTime;    // counting values integer
		Handler myHandler;   // used to delay runnable for a second which delay_RATE
		int delay_RATE = 1000;    //delay
		Java.Lang.Runnable r;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			elapsedTime = 0; // default value
			myHandler = new Handler();     //initializing handler
			r = new Java.Lang.Runnable(Run);

			countTxt = (TextView)FindViewById(Resource.Id.counter);


			resetBtn = ( Button )FindViewById(Resource.Id.reBtn);
			resetBtn.Click += ResetBtn_Click;

			Counter();


		}


		void Counter()
		{
			elapsedTime++;      // increment
			countTxt.Text = elapsedTime.ToString();
			myHandler.PostDelayed(r, delay_RATE);
		}
		void ResetBtn_Click(object sender, System.EventArgs e)
		{
			elapsedTime = 0;    // resetting elapsed time
		}

		void Run()
		{
			Counter();
		}
	}
}

Now Run your android application project.It will start counting number,when application get started.

android runnable,hanlder,thread handler,xamarin,android xamarin,C# xamarin,runnable,run

that’s it for this tutorial.

Thanks.