Menu In Xamarin Android

Introduction

In this article, we are going to learn about how to create a menu in Xamarin Android app.

Solution

Here are the steps to add tabs in an Android app.


Step 1

First, open the solution, add go to Resource, create menu folder and add new item as XML file. For the details, refer to the screenshot given below.

Step 2

Now, go to Values folder and add the lines of code given below in Strings.XML file to add the names of the tabs.

(File Name: Strings.XML) 

  1. <string name="menuItem1">Item 1</string>  
  2.   <string name="menuItem2">Item 2</string>  
  3.   <string name="menuItem3">Item 3</string>   

Step 3

Open the mainMenu.XML file and the code given below to add the menu on the Main Activity, as shown below.

(File Name: mainMenu.XML) 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.   <!-- Add menu Item 1 -->  
  5.   <item android:id="@+id/menuItem1"  
  6.         android:icon="@android:drawable/ic_popup_sync" android:showAsAction="always"/>  
  7.     
  8.   <!-- Add menu Item 2 -->  
  9.   <item android:id="@+id/menuItem2"  
  10.          android:title="@string/menuItem2"/>  
  11.   
  12.   <!-- Add menu Item 3 -->  
  13.   <item android:id="@+id/menuItem3"  
  14.         android:title="@string/menuItem3"/>  
  15.   
  16. </menu>   

Here, android:showAsAction="always" shows the menu item on the Action bar always and rest all menu items are shown in the sub menu.

Step 4

Go to Main Activity and set the menu by adding the code given below.

(File Name: MainActivity.cs) 

  1. public override bool OnCreateOptionsMenu(IMenu menu)  
  2.         {  
  3.             // set the menu layout on Main Activity  
  4.             MenuInflater.Inflate(Resource.Menu.mainMenu, menu);  
  5.             return base.OnCreateOptionsMenu(menu);  
  6.         }   

Step 5

Now, the menu is added on the Main Activity, so we need to add the menu item selected event on the click of menu item. For this, add the code given below.

(File Name: MainActivity.cs) 

  1. public override bool OnOptionsItemSelected(IMenuItem item)  
  2.         {  
  3.             switch (item.ItemId)  
  4.             {  
  5.                 case Resource.Id.menuItem1:  
  6.                     {  
  7.                         // add your code  
  8.                         return true;  
  9.                     }  
  10.                 case Resource.Id.menuItem2:  
  11.                     {  
  12.                         // add your code  
  13.                         return true;  
  14.                     }  
  15.                 case Resource.Id.menuItem3:  
  16.                     {  
  17.                         // add your code  
  18.                         return true;  
  19.                     }  
  20.             }  
  21.   
  22.             return base.OnOptionsItemSelected(item);  
  23.         }   

By adding the action in the selected menu item, you can perform any action on the selection of the menu item.

Output





Similar Articles