How to Create a Menu Step-by-Step for the Android
If you're developing an Android application, you can add useful functionality by providing menus within it. Rather than developers having to implement all of the details of a menu in the Java code for their apps, the platform provides a level of automation. By altering the XML markup in your Android application package, you can instruct the user's device to display standard menus. Android menus can appear when the user interacts with a visible element or when they press the menu button on their device.
Instructions
-
-
1
Open your Android project in your Integrated Development Environment. You can locate the menu resources for your application by browsing to the "res" folder. If you are using Eclipse, which is the recommended IDE for Android, you can access your application directories using the Package Explorer. The menu resources should be placed in a folder named "menu" inside the "res" folder. If your application does not yet have a menu folder, create one by right-clicking on the "res" folder and choosing "New" then "Folder." Enter the name "menu," and click "Finish" in the dialog that appears. Your menu folder should now appear in your application package.
-
2
Create an XML file for your menu. Right-click your menu folder, and choose "New," then "File" to create an XML file. Enter the name of your file -- for example, "my_menu.xml" or a name that reflects the purpose of your menu. Your IDE may output error messages because the content of your menu is not correctly structured yet. Ignore these messages for now. Open the XML file in the text editor of your IDE by double-clicking it in the application directory. You should see your menu XML displayed.
-
-
3
Alter the XML markup for your menu. Inside your menu file, replace the content with the following outline code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>Save your file. Your IDE should stop outputting error messages. This is the outline of a menu in an Android application. Between the opening and closing menu tags, you can place the items you want your menu to present to your users.
-
4
Add items to your menu. Between the menu tags in your XML code, add the following sample code to create two menu items for your application:
<item android:id="@+id/help" android:title="Help" />
<item android:id="@+id/about" android:title="About" />This code determines the text that will appear for each of your menu items. If you want to add icons, you can store them in your application's "drawable" folder, altering your XML code as follows:
<item android:id="@+id/about" android:title="About" android:icon="@drawable/about" />Alter the code to match the name of your icon image files.
-
5
Instruct Java to create your menu. In the Java class file for the Activity you want your menu to appear in, you need to instruct the application to display the menu items you defined in your XML code. Open the Java file for the Activity and add the following method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_menu, menu);
return true;
}This code instructs the application to use the menu outlined in the "my_menu" XML file.
-
1
Tips & Warnings
You can also create context menus, which appear when a user long-presses an item in your application.
To implement what should happen when users interact with your menu items, you need to provide the "onOptionsItemSelected" method.
References
Resources
- Photo Credit Photos.com/PhotoObjects.net/Getty Images