Download Code

Xamarin Runnable Thread Handler Android

In this tutorial you will learn how to implement Xamarin Runnable Thread Handler Android,to run code in a separate thread.

You can read basics of thread in android application from here.


Runnables is used to represent a command that can executed.

It is mostly to used to run any module in different threads.

Thread Handler

A Handler is used to send,process Message and Runnable object’s associated with the Thread’s MessageQueue. Every.Every handler instance associated with the single thread and it’s message queue.

There are two main uses for a Handler:-

  • To schedule messages and runnables to be executed as some point in the future
  • To enqueue an action to be performed on a different thread than your own.


Looper is used to run message loop for a thread.By default threads don’t have message loop,to create that call prepare() method,and then loop() method to process messages until loop end.

Basic Example of Looper

class secondActivity extends Thread {
      public Handler mHandler;

      public void run() {

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here


OK let’s start with the thread example using Runnable class.In this project we will deal with current and background Thread.

Create Android Application Project.

Now go to your Main.axml and use two buttons and a Text-view in xml.

Xamarin Runnable,Xamarin Runnable Thread Handler,Xamarin Runnable Thread Handler Android,Runnable Thread Handler Android

Now go to your MainActivity.cs class.Create Button and TextView variables,refer their id’s and apply click.

Now add Runnable class code and call runnable run method to start thread.

using Android.App;
using Android.Widget;
using Android.OS;

using System;
using Android.Content;
using Android.Views;
using System.Threading;
using Java.Lang;

namespace XamarinRunnableThread
	[Activity(Label = "XamarinRunnableThread", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
		Button threadStart, threadRun; // Button variables
		TextView Info;                 // textview to display information about thread
		Handler h;
		Action a;

		Java.Lang.Runnable m;

		protected override void OnCreate(Bundle savedInstanceState)

			// Set our view from the "main" layout resource
			m = new Java.Lang.Runnable(Run);
			Info = ( TextView )FindViewById(Resource.Id.threadInfo);
			threadStart = ( Button )FindViewById(Resource.Id.startBtn);
			threadStart.Click += delegate



			threadRun = ( Button )FindViewById(Resource.Id.runBtn);
			threadRun.Click += delegate

				RunOnUiThread(() => Info.Text = "in background thread");



		void Run()
				Info.Text = "in main thread";



Run your Xamarin Runnable Thread Handler android application project.

Xamarin Runnable,Xamarin Runnable Thread Handler,Xamarin Runnable Thread Handler Android,Runnable Thread Handler Android
Basically in Xamarin Runnable Thread Handler android tutorial we used Runnable class to run something on current and UI Thread.
that’s it for this tutorial.