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?
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.
You can inflate a xml menu in multiple locations. A few examples:
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 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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With