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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.