In Android application, menus are the common user interface components which gives user a dynamic user experience. The other details about menus are:
- android.view.Menu is class which provides the functionality of menu.
- Till the Android version 2.3 , dedicated menu hardware button is provided for the menu option.
- After Android version 3.0 and higher, no dedicated button is provided but instead another ways are provided according to type of menu to access the menu.
- There are three types of Menus or action used in all version of menus.
- Option menu and action bar-It is presented by action bar as a combination of on-screen action bar and overflow options.
- Context menu and contextual action mode-It is presented by contextual action mode for enabling the menu and selected item.
- Pop up menu
so now lets learn how to add Option menus in our android application.
- First start your eclipse and select new -> project and click next.
- Now fill with following details:
- Project name- OptionMenu
- Application name-OptionMenu
- package name-option.menu
- Minimum required SDK- select android 3.0
- tick on create custom laucher icon.
- Name the activity -Optionmenu
- tick on create project in workspace.
- In Config Launcher Icon window click Next and finally choose Blank Activity in Create Activity window and the click Next and then Finish.
- Now for creating menu we will use xml format to define menu item.so open.so for creating the xml file open the project OptionMenu-> res-> menu and right click on menu sub-folder and select the new file name it as mainmenu.xml.
- Now open the mainmenu.xml file and add the following xml lines in the file.
1: <menu xmlns:android="http://schemas.android.com/apk/res/android">
2: <item android:id="@+id/facebook"
3: android:title="@string/facebook"
4: android:icon="@drawable/facebook">
5: </item>
6: <item android:id="@+id/wordpress"
7: android:title="@string/wordpress"
8: android:icon="@drawable/wordpress">
9: </item>
10: <item android:id="@+id/google"
11: android:title="@string/google"
12: android:icon="@drawable/google" android:showAsAction="ifRoom">
13: </item>
14: </menu>
- Now let's understand the details of above mainmenu.xml lines.
- At line 1,we have added first menu item in which we have given title and icon for the menu.
- Title is defined in the string.xml file.
- Icon image is given in the drawable folder.
- Similarly we have added three menu item in th emainmenu.xml file.
- At line 12 ,android:showAsAction="ifRoom" is added which means it will appear in the action bar to provide quick access.
- Now open the strings.xml file by clicking res-> values-> strings.xml and the few lines to it so that it finally look like this.In the file given below we have added the three Title string for the menu like Facebook, Google., WordPress.
1: <resources>
2: <string name="app_name">OptionMenu</string>
3: <string name="hello_world">Hello world!</string>
4: <string name="menu_settings">Settings</string>
5: <string name="title_activity_main">MainActivity</string>
6: <string name="facebook">facebook</string>
7: <string name="wordpress">wordpress</string>
8: <string name="google">google</string>
9: </resources>