Prerequisite

If you not understand this lecture Option Menu in android studio.You should also read my previous lectures Creating a time picker in android and Toggle button in android studio . After reading these lecture you will also able to create Option Menu of your app.

Overview

Menus are used in applications to handle some common functionality around the application.The options menu is the primary collection of menu items for an activity.You can also set the color for the option menu and its main items.

Introduction

In this lecture we will describe how can create Option menu in android and also describe how can handle click events in option menu.And also describe contextual Menu.  why we can use it.After Reading this lecture you will understand Option menu in android And i hope you will create Option menu of your application.

Description of option menu

Android menu is used to show multiple options for the application. when the button is pressed. Menu are show on that activity.Menu consists items and sub item.You can add, insert, and remove option menu.In this lecture we will describe how can create a option menu in android .Option menu is available in API 3.0 and above .But lower 3.0 API  option menu button is available instead of option menu.So we create a project in android studio and make sure that minimum SDK  is 3.0 this is why because we add a menu bar in Activity. Menu are available in res folder you can expand the res folder and click on menu folder and expand the menu folder. You can see menu. XML file is available in menu folder.This statement is used to show menu.

For more detail about android menu and option  click on Option menu in android. First of all you will create a new project in android studio.  Every project contain two file first is java class file and second is layout XML file .For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity’s code, you should define a menu and all its items in an XML.For menus layout you will create a separate layout . how can create menu layout detail is provides step by step.

Step 1=Add menu<-Right click the menu folder-><- select new option->and click on-<Menu resource folder->

Alt Tag android add new menu.xml image

Step2=Step1<-Writename->ok

Alt tag android menu assign name menu

After that you will check it  new menu.XML file  will add in menu folder. All the menu items will add in new menu.xml file.you can add items and assign id to each item and  also assign title of each item. how can add  title of each item. you will open a string.XML file and add string resource in string .XML file.

Code that are write in string resource.xml file

Code that are write in menu.xml file

For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code  you should define a menu and all its items in an XML.

Code that are write MainActivity. java class file.

Run the application android  Date Picker

Alt Tag android menu output image

Handling click Events of option menu

For handling on click method of the menu item override the onOptionsItemSelected(MenuItem item)method in your activity. This method invoke whenever user click/select any menu item. For handling different actions for each element get the id of the selected menu item using getItemId() method and perform action for each menu item.Each activity can has its own menu. You can put different menu items for each activity and handle it differently. Some activity cannot have any menu.

Android 3.0 provides facility to define on-click on a menu .XML file.Using on click method you can change the background color of your activity.For selecting a option in option menu you can move from one activity to other. Method  that are used  must have public and only single parameter is used.when the system calls this method, it passes the menu item that are selected.if your application contain multipal activity in which many activity have same option menu then you will use these method.

 

Creating Contextual Menu

Contextual menus used to select one option menu.Context Menu displays hierarchical data as a multi level menu in a popup.It provides option of items and user can select one option at a time.

Handling click Events of floating contextual menu

A menu display as a floating list of menu items such as dialog box. when user click on button or long press the floating contextual menu display option.So we will describe how can handle click events in floating contextual menu.The example that we describe consists three option delete share and help when user click or select option dialog box appear.First of all you will create a new project in android studio.  Every project contain two file first is java class file and second is layout XML file .For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity’s code, you should define a menu and all its items in an XML.For menu layout you will create a separate layout. how can create a menu layout you will  see step1 and step2.

Code that are write in string resource.xml file

Code that are write in menu.xml file

For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code  you should define a menu and all its items in an XML.

Code that are write in Layout.xml file

Now you may open the Graphical layout editor to preview the User Interface you created of List view  in android.

Alt Tag android list view in android

Code that are write MainActivity. java class file.

Run the application

Alt Tag android menu output

when you long press in a list view then a dialog box is appear show in a below pic

.Alt Tag android menu

Thank you  for reading this lecture Hope you got the idea.