12

I want to create a simple option menu in Android application with c# and Xamarin Studio. How can I do it?

I haven't found any C# example of this. Can someone simply explain how to create a option menu, please?

2
  • 3
    I think that after a couple edits, this is a real and valid question. Why not reopen it? Jan 2, 2014 at 12:22
  • Agree, actually I found the answer helpful.
    – Aoi Karasu
    Mar 20, 2014 at 0:32

1 Answer 1

33

Defining the menu

One way to create a menu is using a XML file placed in the Resources/menu/ folder of your Xamarin.Android project.

For example:

Resources/Menu/mymenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/item1"
        android:title="Item 1"/>
  <item android:id="@+id/item2"
        android:title="Item 2"/>
  <item android:id="@+id/item3"
        android:title="Item 3"/>
</menu>

To see what other options you can define in a menu xml file, please see the official documentation.

Using the menu

You can inflate a xml menu in multiple locations. A few examples:

In a Toolbar

Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.mytoolbar);
toolbar.InflateMenu(Resource.Menu.mymenu);

Handle clicks

To handle click events on a toolbar menu you have to implement the Toolbar.IOnMenuItemClickListener interface by overriding the following method:

public bool OnMenuItemClick(IMenuItem item)
{
    switch (item.ItemId)
    {
        case Resource.Id.item1: 
                //Do stuff for item1
                return true;
        case Resource.Id.item2: 
                //Do stuff for item2
                return true;
        case Resource.Id.item3:
                //Do stuff for item3
                return true;
        default:
                return false;
     }
}

You then have to add the class implementing the interface to the toolbar as a listener:

toolbar.SetOnMenuItemClickListener(your_listener_class);

In the default menu location of an Activity or Fragment (DEPRECATED)

In most cases the default menu location of an activity or fragment is either the hardware menu button or the ActionBar. Adding a menu here can be accomplished by the overriding the following method:

In a Activity:

public override bool OnCreateOptionsMenu(IMenu menu)
{
    MenuInflater.Inflate(Resource.Menu.mymenu, menu);
    return true;
}

In a Fragment:

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
    inflater.Inflate(Resource.Menu.mymenu, menu);
}

Make sure you have HasOptionsMenu set to true in the onCreate of the Fragment for this to work.

Handle clicks

You can then handle clicks to the menu by overriding OnOptionsItemSelected

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
       case Resource.Id.item1: 
                //Do stuff for item1
                return true;
        case Resource.Id.item2: 
                //Do stuff for item2
                return true;
        case Resource.Id.item3:
                //Do stuff for item3
                return true;
        default:
                return false;
     }
}

After we have handled the selected item we return true to notify the system of this.

Alternative: Creating a menu programatically

A very basic menu is accomplished by overriding the OnCreateOptionsMenu method like this:

public override bool OnCreateOptionsMenu(IMenu menu)
{
      menu.Add(0,0,0,"Item 0");
      menu.Add(0,1,1,"Item 1");
      menu.Add(0,2,2,"Item 2");
      return true;
}

You can then handle clicks in the option menu by overriding the OnOptionsItemSelected method.

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
        case 0: 
                //Do stuff for item 0
                return true;
        case 1: 
                 //Do stuff for item 1
                return true;
        case 2: 
                //Do stuff for item 2
                return true;
        default:
                return false;
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.