Menus

Create menu resources to further decouple your presentation layer by designing your menu layouts in XML rather than constructing them in code.

Menu resources can be used to define both Activity and context menus within your applications, and provide the same options you would have when constructing your menus in code. Once defined in XML, a menu is "inflated" within your application via the inflate method of the Menuinflator Service, usually within the onCreateOptionsMenu method. You will examine menus in more detail in Chapter 4.

Each menu definition is stored in a separate file, each containing a single menu, in the res/menu folder. The file name then becomes the resource identifier. Using XML to define your menus is best-practice design in Android.

A thorough explanation of menu options is included in the next chapter, but Listing 3-3 shows a simple menu example.

LISTING 3-3: Simple menu layout resource Available for downloadon <?xml version="1.0" encoding="utf-8"?>

Wrox.com <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_refresh"

android:title="Refresh" /> <item android:id="@+id/menu_settings" android:title="Settings" />

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment