Download Code

Xamarin Toast Example Android

In this tutorial you will learn how to create xamarin toast example android application.First let me give some introduction about Xamarin Toast.

Xamarin Toast

A small popup feedback message which display to the user when interacting with UI component like when click on button it will display “Android Codec”.Some people mostly call it popup notification.You can also set it’s position to left,top,right etc.More Detailed.

Delegate

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

I hope you read previous tutorial in which you created new xamarin android application project.So,just no need to tell you those steps.

OK let’s start by create new android application project.Go to File > New > Solution.A Window will open just like this.

toast,toast xamarin,toast in xamarin android,Xamarin toast,Xamarin Toast Example,Toast Example Android,Xamarin toast example android

Now select Android App and hit next.Then give android application name and set target platform to “Modern Development” and hit next.Theme remains the same like “DEFAULT”.Then configure your project and hit create.After creating android project it will look like this by default.

toast,toast xamarin,toast in xamarin android,Xamarin toast,Xamarin Toast Example,Toast Example Android,Xamarin toast example android

Ok Now go to Resources > Layout directory and open Main.axml.It has basically Two Views.

  • Designer
  • Source

It will look like this.

toast,toast xamarin,toast in xamarin android,Xamarin toast,Xamarin Toast Example,Toast Example Android,Xamarin toast example android

I’ll use just use one button to display message.So,After that  i’ll go to MainActivity.cs class.From Scratch it will look like this.

namespace toast
{
	[Activity (Label = "toast", MainLauncher = true, Icon = "@mipmap/icon")] // contains title of an app and icon
	public class MainActivity : Activity       // main class and inherited Activity class using ':' this operator.
	{
		// oncreate method from where execution starts
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
		}
	}
}

Next use SetContentView() method to set axml file to view.Now Create Button variable and refer axml button id.After creating variable and referring a button it will look like this.

namespace toast
{
	[Activity (Label = "toast", MainLauncher = true, Icon = "@mipmap/icon")] // contains title of an app and icon
	public class MainActivity : Activity       // main class and inherited Activity class using ':' this operator.
	{
		Button codeClick;   // button variable named codeClick
		// oncreate method from where execution starts
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			SetContentView (Resource.Layout.Main);    // setting axml view
			codeClick = FindViewById < Button > (Resource.Id.myButton);   // referring button
		}
	}
}

Next apply click event on button to display popup message using Delegates and add popup message.After this your MainActivity.cs class should look like this.

MainActivity.cs

/* All Right Reserved By Farhan Rasheed */

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

namespace toast
{
	[Activity (Label = "toast", MainLauncher = true, Icon = "@mipmap/icon")] // contains title of an app and icon
	public class MainActivity : Activity       // main class and inherited Activity class using ':' this operator.
	{
		Button codeClick;   // button variable named codeClick
		// oncreate method from where execution starts
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			SetContentView (Resource.Layout.Main);    // setting axml view
			codeClick = FindViewById< Button > (Resource.Id.myButton);   // referring button
			codeClick.Click += delegate(object sender, System.EventArgs e) {
				Toast.MakeText(this,"Welcome to Android Codec Xamarin",ToastLength.Short).Show();
			};
		}
	}
}

Now first select API level which on left-top corner and then hit button.Or go to Run > Start without debugging.Output will be like this.

toast,toast xamarin,toast in xamarin android,Xamarin toast,Xamarin Toast Example,Toast Example Android,Xamarin toast example android

That’s it for Xamarin toast example android application tutorial.I hope you get every step clearly.

Thanks.