Dynamically Updating Menu Items

By overriding your Activity's onPrepareOptionsMenu method you can modify a Menu based on an application's current state immediately before the Menu is displayed. This lets you dynamically disable/enable Menu Items, set visibility, and modify text.

To modify Menu Items dynamically you can either find a reference to them in the onCreateOptionsMenu method when they're created, or you can use the findItem method on the Menu object, as shown in Listing 4-27, where onPrepareOptionsMenu is overridden.

LISTING 4-27: Dynamic menu modification Available for download on ©Override

Wrox.com public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu);

MenuItem menuItem = menu.findItem(MENU_ITEM);

return true;

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