How to Create Menus in Mono Android

In android application menu is one of the important user interface entity which provides some action options for a particular view. In this tutorial i am discussing about creating android menus.

In this tutorial we are creating a simple menu with 6 menu items. On clicking on single menu item a simple Toast message will be shown.
1. Create a new project File ⇒ New ⇒ Android Project and give activity name asAndroidMenusActivity.
2. Now create an XML file under res/layout folder and name it as menu.axml.
3. Open menu.axml file and type following code. In the following code we are creating a single menu with 6 menu items. Each menu item has an icon and title for display the label under menu icon. Also we have id for each menu item to identify uniquely.
 menu.axml
<?xml version="1.0" encoding="utf-8"?>
    <!-- Single menu item
         Set id, icon and Title for each menu item
    -->
    <item android:id="@+id/menu_bookmark"
          
          android:title="Bookmark" />
    <item android:id="@+id/menu_save"
          
          android:title="Save" />
    <item android:id="@+id/menu_search"
       
          android:title="Search" />
    <item android:id="@+id/menu_share"
          
          android:title="Share" />
    <item android:id="@+id/menu_delete"
         
          android:title="Delete" /> 
    <item android:id="@+id/menu_preferences"
          
          android:title="Preferences" />
</menu>




Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical" />



4. Now open your main Activity class file (Activity.cs) and type following code. In the following code each menu item is identified by its ID in switch case statement.
Activity.cs
namespace Menu_dsiplay
{
    [Activity(Label = "Menu_dsiplay", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater menuInflater = MenuInflater;// getMenuInflater();
            menuInflater.Inflate(Resource.Layout.menu, menu);
            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {

            switch (item.ItemId)
            {
                case Resource.Id.menu_bookmark:
                    Toast.MakeText(this, "Bookmark is Selected", ToastLength.Long).Show();
                    return true;

                case Resource.Id.menu_save:
                    Toast.MakeText(this, "Save is Selected", ToastLength.Long).Show();
                    return true;

                case Resource.Id.menu_search:
                    Toast.MakeText(this, "Search is Selected", ToastLength.Long).Show();
                    return true;

                case Resource.Id.menu_share:
                    Toast.MakeText(this, "Share is Selected", ToastLength.Long).Show();
                    return true;

                case Resource.Id.menu_delete:
                    Toast.MakeText(this, "Delete is Selected", ToastLength.Long).Show();
                    return true;

                case Resource.Id.menu_preferences:
                    Toast.MakeText(this, "Preferences is Selected", ToastLength.Long).Show();
                    return true;

                default:
                    return base.OnOptionsItemSelected(item);
            }

            return base.OnOptionsItemSelected(item);
        }
    }
}
5. Finally run your project. On Android Emulator click on Menu Button to launch menu.


Do you want more TechChaitu Updates ?
Enter your email address:

Comments

Popular posts from this blog

Download Visakhapatnam Tirupati AC Double Decker Express Act