O'Reilly logo

Pro Android 4 by Satya Komatineni, Dave MacLean

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required