Loading Menus Through XML Files

Up until this point, we've created all our menus programmatically. This is not the most convenient way to create menus, because for every menu, you have to provide several IDs and define constants for each of those IDs. No doubt this is tedious.

Instead, you can define menus through XML files, which is possible in Android because menus are also resources. The XML approach to menu creation offers several advantages, such as the ability to name menus, order them automatically, and give them IDs. You can also get localization support for the menu text.

Follow these steps to work with XML-based menus:

  1. Define an XML file with menu tags.
  2. Place the file in the /res/menu subdirectory. The name of the file is arbitrary, ...

Get Pro Android 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.