Xamarin Android: Create Android Custom Action Bar Menu And Popup Menu

Let’s start. 

Step 1: Open Visual Studio ->New Project ->Templates ->Visual C# ->Android ->Blank App.

Select Blank App and give the Project Name and Project Location.

CUSTOM ACTION BAR MENU

Step 2: Next, create options_menu.xml file. Go to Solution Explorer -> Project Name ->Resources ->values. Then, right click and go to Add->New Item. This opens a new dialog box. Select XML File and give it a name, as option_menu.xml.



Step 3
: The next step is to open the Solution Explorer -> Project Name ->Resources ->values ->options_menu.xml. Click on Open Design View and then, give the following code in it.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  
  3.     <!--///showAsAction="always" ///-->  
  4.     <item android:id="@+id/action_settings" android:title="Share" showAsAction="always" />  
  5.     <item android:id="@+id/action_settings1" android:title="Bluetooth" showAsAction="always" />  
  6.     <item android:id="@+id/action_settings2" android:title="Exit" showAsAction="always" />  
  7.     <!--/android:showAsAction="ifRoom"/-->  
  8.     <item android:id="@+id/action_settings3" android:title="Share" android:showAsAction="ifRoom" />  
  9.     <item android:id="@+id/action_settings4" android:title="Bluetooth" android:showAsAction="ifRoom" />   
  10. </menu>  

Step 4: Next, open Solution Explorer -> Project Name ->MainActivity.cs. Create OnCreateOptionsMenu() after Oncreate() method.

  1. public override bool OnCreateOptionsMenu(IMenu menu)  
  2. {  
  3.     MenuInflater.Inflate(Resource.Menu.option_menu, menu);  
  4.     return true;  
  5. }  

POPUP MENU CREATION

Step 5: Now, create another xml file as options_popup.xml file. For this, go to Solution Explorer -> Project Name ->Resources ->values. Right click and Add ->New Item. Then, open new dialog box. Then, select XML File and give it the name as popup_menu.xml.



Step 6
: Next, open the Solution Explorer -> Project Name ->Resources ->values -> popup_menu.xml. Click on Open Design View. Then, give the following code:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  
  3.     <item android:id="@+id/action_setting1" android:title="Share" showAsAction="always" />  
  4.     <item android:id="@+id/action_settings2" android:title="Bluetooth" showAsAction="always" />  
  5.     <item android:id="@+id/action_settings3" android:title="Exit" showAsAction="always" />   
  6. </menu>  

Step 7: Next step is to open the Solution Explorer -> Project Name ->Resources ->Layout ->Main.axml. Then, give the following code.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  3.     <Button android:id="@+id/btnpopupmenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="POPUP MENU" />   
  4. </LinearLayout>  

Step 8: Now, open Solution Explorer -> Project Name -> MainActivity.cs. Then, give the following code:

 

  1. protected override void OnCreate(Bundle bundle)   
  2. {  
  3.     base.OnCreate(bundle);  
  4.     // Set our view from the "main" layout resource  
  5.     SetContentView(Resource.Layout.Main);  
  6.     Button btnpopupmenu = FindViewById < Button > (Resource.Id.btnpopupmenu);  
  7.     btnpopupmenu.Click += (s, arg) =>  
  8.     {  
  9.         PopupMenu menu = new PopupMenu(this, btnpopupmenu);  
  10.         menu.Inflate(Resource.Menu.popup_menu);  
  11.         menu.Show();  
  12.     };  
  13. }  

Step 9: Press F5 or Build and Run the Application.



Finally, we have successfully created the Xamarin Android Custom Action Bar Menu and created the Popup Menu.