Creating Your First Menu
You can create a menu through code or you can create it through an XML file that is provided in the res/menu directory. The preferred method of creating menus is to define menus through XML and then inflate them into a programmable object with which you can interact. (Inflate here refers to the Android process of turning an XML file into a Java object.) This helps separate the menu definition from the actual application code.
Defining the XML file
To define an XML menu, follow these steps:
- Create a menu folder in the res directory.
- Add a file by the name of list_menu.xml to the menu directory.
- Type the code from Listing 12-1 into the list_menu.xml file.
<?xml version=“1.0” encoding=“utf-8”?> <menu xmlns:android=“http://schemas.android.com/apk/res/android”> <item android:id=“@+id/menu_insert” android:icon=“@android:drawable/ic_menu_add” →4 android:title=“@string/menu_insert” /> →5 </menu>
Notice that a new string resource is included in line 5. You need to create that (which you do in Step 4). The android:icon value in line 4 is a built-in Android icon. You do not have to provide this bitmap in your drawable resources. The ldpi, mdpi, and hdpi versions of this icon are all built into the Android platform. To view other resources available to you, view the android.R.drawable documentation here: http://developer.android.com/reference/android/R.drawable.html.
All resources in the android.R class are available ...