Download Code

Xamarin PopUp Menu Example Android

In this tutorial you will learn how to create xamarin PopUp Menu example android application using Xamarin IDE.

First i’ll give you some basic information about PopUp Menu.

Xamarin PopUp Menu

The PopUp menus class is basically used to display list of menus items in one View.

So, let’s start by creating new android application project.File > New > Solution > Android App name it and hit Next > Create button.

First right click on project Resources > Add > New Folder.Name it as Menu.

menus,Menu,android pop up menus,popup menu,Xamarin PopUp Menu,Xamarin PopUp Menu example,Xamarin PopUp Menu example android,PopUp Menu Example android

Now right click on that directory Menu > Add  > New File.A new window will open go to XML option on bottom left for empty XML file and give name to this xml.

menus,Menu,android pop up menus,popup menu,Xamarin PopUp Menu,Xamarin PopUp Menu example,Xamarin PopUp Menu example android,PopUp Menu Example android

After creating xml file add xml code with menus items.

menus,Menu,android pop up menus,popup menu

We are done with our menus layout.Now go to your MainActivity.cs file.Hit Alt+Insert button a window will open go to Override Members option and select two method from below.

menus,Menu,android pop up menus,popup menu,Xamarin PopUp Menu,Xamarin PopUp Menu example,Xamarin PopUp Menu example android,PopUp Menu Example android

After implementing these method your MainActivity.cs class will be like.

namespace Menu
{
	[Activity (Label = "Menu", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		int count = 1;

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

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById< Button > (Resource.Id.myButton);
			
			button.Click += delegate {
				button.Text = string.Format ("{0} clicks!", count++);
			};
		}
		public override bool OnCreateOptionsMenu (Android.Views.IMenu m)
		{
			return base.OnCreateOptionsMenu (m);
		}

		public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
		{
			return base.OnOptionsItemSelected (item);
		}


	}
}

OK Now we will use MenuInflater class.This class is basically used to instantiate menu xml files into menus object.Next load menus xml file using inflate method and apply switch cases in OnOptionsItemSelected method.

MainActivity.cs

/* All Rights Reserved By Farhan Rasheed */
using Android.App;
using Android.Widget;
using Android.OS;

namespace Menu
{
	[Activity (Label = "Menu", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		int count = 1;

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

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById< Button > (Resource.Id.myButton);
			
			button.Click += delegate {
				button.Text = string.Format ("{0} clicks!", count++);
			};
		}
		public override bool OnCreateOptionsMenu (Android.Views.IMenu me)
		{
			MenuInflater.Inflate(Resource.Menu.codecmenu, me);
			return true;
		}

		public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
		{
			switch (item.ItemId)
			{
			case Resource.Id.item1: 
				
				Toast.MakeText(this,"Android",ToastLength.Short).Show();
				return true;
			case Resource.Id.item2: 
				
				Toast.MakeText(this,"IO's",ToastLength.Short).Show();
				return true;
			case Resource.Id.item3:

				Toast.MakeText(this,"Windows",ToastLength.Short).Show();
				return true;
			case Resource.Id.item4:
				
				Toast.MakeText(this,"BlackBerry",ToastLength.Short).Show();
				return true;
			default:
				return false;
			}

		}


	}
}



Now run your xamarin popup menu example android application project.Output should be like these screen.

menus,Menu,android pop up menus,popup menu,Xamarin PopUp Menu,Xamarin PopUp Menu example,Xamarin PopUp Menu example android,PopUp Menu Example android

that’s it for simple menus tutorial.

Thanks.