SlideShare a Scribd company logo
Android UI - Menus
Topics
• Types of menus
• Options menu
• Context menu
• Submenu
• Creating menu using Menu resource
Types of Menus
Types of Menus
• Context menu
> Floating list of menu items that may appear when
you perform a long-press on a View
• Options menu
> Revealed when the device MENU key is pressed
• Submenu
> Used to organize menu items into groups
> A Submenu item does not support nested Submenus
Context Menu
Context Menu
• Context menus do
not support item
shortcuts and item
icons.
How to Create Context Menu?
• When Context menu is opened for the first
time, the Android system will call the Activity's
onCreateContextMenu(Menu menu) callback
method.
> Override this method in your Activity class and
populate the Menu object given to you with
MenuItem's.
• You can populate the menu in two ways
> Scheme #1: by calling add() for each item you'd like
in the menu.
> Scheme #2: by inflating a menu resource that was
defined in XML (preferred)
Populating Menu with Menu Items: #1
// Override this method of Activity class in order to create menu items.
@Override
public void onCreateContextMenu(
ContextMenu menu, // Context menu that is being built
View view, // The view for which the context menu is being built
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderTitle("Context menu");
menu.add(0, Menu.FIRST, Menu.NONE, "menu #1");
menu.add(0, Menu.FIRST + 1, Menu.NONE, "menu #2");
menu.add(0, Menu.FIRST + 2, Menu.NONE, "menu #3");
menu.add(0, Menu.FIRST + 3, Menu.NONE, "menu #4");
}
How to handle Menu Selection?
• When a menu item is selected from the Context
Menu, onContextItemSelected() callback
method of your Activity gets called
> This callback passes you the MenuItem that has been
selected.
> You can identify the item by requesting the itemId,
with getItemId(), which returns the integer that was
assigned with the add() method.
> Once you identify the menu item, you can take an
appropriate action.
Example: Handling Menu Selection
/* Handles item selections */
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_NEW_GAME:
newGame();
return true;
case MENU_QUIT:
quit();
return true;
}
return false;
}
Options Menu
When to use Options Menu?
• The Options Menu is
where you should
include basic
application functions
and any necessary
navigation items
(e.g., to a home
screen or application
settings).
How Options Menu Work?
• The Options Menu is opened by pressing the
device MENU key.
• When opened, the Icon Menu is displayed,
which holds the first six menu items.
• If more than six items are added to the Options
Menu, then those that can't fit in the Icon Menu
are revealed in the Expanded Menu, via the
"More" menu item.
Populating Menu with Menu Items: #1
/* Creates the menu items without Icons */
public boolean onCreateOptionsMenu(Menu menu) {
// The add() method used in this sample takes four arguments:
// groupId, itemId, order, and title.
menu.add(0, MENU_NEW_GAME, 0, "New Game");
menu.add(0, MENU_QUIT, 0, "Quit");
return true;
}
/* Creates the menu items with Icons. Note that add() method returns
newly created MenuItem object to set additional properties like an icon,
a keyboard shortcut, an intent, and other settings for the item. */
public boolean onCreateOptionsMenu(Menu menu) {
// The add() method used in this sample takes four arguments:
// groupId, itemId, order, and title.
menu.add(0, MENU_NEW_GAME, 0,
"New Game").setIcon(R.drawable.menu_new_game_icon);
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
return true;
}
How to handle Menu Selection?
• When a menu item is selected from the Options
Menu, onOptionsItemSelected() callback
method of your Activity gets called
> This callback passes you the MenuItem that has been
selected.
> You can identify the item by requesting the itemId,
with getItemId(), which returns the integer that was
assigned with the add() method.
> Once you identify the menu item, you can take an
appropriate action.
Example: Handling Menu Selection
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_NEW_GAME:
newGame();
return true;
case MENU_QUIT:
quit();
return true;
}
return false;
}
Submenu
When to use SubMenu?
• If you have several menu items that can be
grouped together with a title, consider
organizing them into a Submenu.
Example: Creating Submenu
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
// Create submenu "File"
SubMenu fileMenu = menu.addSubMenu("File");
fileMenu.add("New");
fileMenu.add("Open File");
fileMenu.add("Close");
fileMenu.add("Close All");
// Create submenu "Edit"
SubMenu editMenu = menu.addSubMenu("Edit");
editMenu.add("Undo Typing");
editMenu.add("Redo");
editMenu.add("Cut");
return result;
}
Creating Menu using
Menu Resource
Why using Menu Resource?
• Instead of instantiating Menu objects in your
application code, you should define a menu and
all its items in an XML menu resource, then
inflate the menu resource (load it as a
programmable object) in your application code.
• Defining your menus in XML is a better practice
(than creating them in code) because it
separates your interface design from your
application code (the same as when you define
your Activity layout in XML).
When to Use Menu Resource File?
• Create <menu_resource>.xml under res/menu/
directory
• Inflate the Menu Resource file using
inflate(<menu-resource-id>) method of the
MenuInflator class
> Menu objects are created from the Menu resource
file
Example: Inflating Menu Resource
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.title_only, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.jump:
Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_LONG)
.show();
return true;
case R.id.dive:
Toast.makeText(this, "Dive into the water!", Toast.LENGTH_LONG)
.show();
return true;
}
Example: Menu Resource File
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/jump"
android:title="Jump!"
android:icon="@drawable/draw_jump" />
<item android:id="@+id/dive"
android:title="Dive!"
android:icon="@drawable/draw_dive" />
</menu>
Thank you

More Related Content

What's hot (20)

PPT
android menus
Deepa Rani
 
DOCX
Android menus in android-chapter15
Dr. Ramkumar Lakshminarayanan
 
PDF
Android Threading
Jussi Pohjolainen
 
ODP
Android App Development - 05 Action bar
Diego Grancini
 
PPTX
Windowforms controls c#
prabhu rajendran
 
PDF
Android intents
Siva Ramakrishna kv
 
PPTX
Popup boxes
sonal bisla
 
PPTX
Android Widget
ELLURU Kalyan
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPT
Versions of android
Kartik Kalpande Patil
 
PDF
Basics of JavaScript
Bala Narayanan
 
PDF
Android datastorage
Krazy Koder
 
PDF
Fragments In Android
DivyaKS12
 
PPTX
I/O Streams
Ravi Chythanya
 
PDF
Android activities & views
ma-polimi
 
PPTX
What and Why Flutter? What is a Widget in Flutter?
MohammadHussain595488
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPT
Servlets
Sasidhar Kothuru
 
android menus
Deepa Rani
 
Android menus in android-chapter15
Dr. Ramkumar Lakshminarayanan
 
Android Threading
Jussi Pohjolainen
 
Android App Development - 05 Action bar
Diego Grancini
 
Windowforms controls c#
prabhu rajendran
 
Android intents
Siva Ramakrishna kv
 
Popup boxes
sonal bisla
 
Android Widget
ELLURU Kalyan
 
Javascript variables and datatypes
Varun C M
 
Versions of android
Kartik Kalpande Patil
 
Basics of JavaScript
Bala Narayanan
 
Android datastorage
Krazy Koder
 
Fragments In Android
DivyaKS12
 
I/O Streams
Ravi Chythanya
 
Android activities & views
ma-polimi
 
What and Why Flutter? What is a Widget in Flutter?
MohammadHussain595488
 
Exception Handling in Java
lalithambiga kamaraj
 

Viewers also liked (20)

PDF
Menu in android
Durai S
 
PDF
Action Bar and Menu
Katsumi Onishi
 
PDF
Action Bar in Android
Prof. Erwin Globio
 
PDF
04 user interfaces
C.o. Nieto
 
PDF
Android in practice
Jose Manuel Ortega Candel
 
PDF
Android Training - Sliding Menu
Kan-Han (John) Lu
 
PDF
Android location
Krazy Koder
 
PPT
Lecture Slides for Preferences and Menus [Android ]
Nehil Jain
 
PDF
Android appwidget
Krazy Koder
 
PPTX
Android Notifications in Android Nougat 7.0
Gracia Marcom
 
PDF
Android L04 - Notifications and Threading
Mohammad Shaker
 
PPT
2310 b xd
Krazy Koder
 
PDF
Alertdialog in android
Durai S
 
PDF
Introduction to fragments in android
Prawesh Shrestha
 
PDF
Android UI Development
Jussi Pohjolainen
 
ODP
Android App Development - 06 Fragments
Diego Grancini
 
PDF
Android Location and Maps
Jussi Pohjolainen
 
PDF
Android notification
Krazy Koder
 
PPTX
Event Handling in java
Google
 
PPT
Introduction to Android Fragments
Sergi Martínez
 
Menu in android
Durai S
 
Action Bar and Menu
Katsumi Onishi
 
Action Bar in Android
Prof. Erwin Globio
 
04 user interfaces
C.o. Nieto
 
Android in practice
Jose Manuel Ortega Candel
 
Android Training - Sliding Menu
Kan-Han (John) Lu
 
Android location
Krazy Koder
 
Lecture Slides for Preferences and Menus [Android ]
Nehil Jain
 
Android appwidget
Krazy Koder
 
Android Notifications in Android Nougat 7.0
Gracia Marcom
 
Android L04 - Notifications and Threading
Mohammad Shaker
 
2310 b xd
Krazy Koder
 
Alertdialog in android
Durai S
 
Introduction to fragments in android
Prawesh Shrestha
 
Android UI Development
Jussi Pohjolainen
 
Android App Development - 06 Fragments
Diego Grancini
 
Android Location and Maps
Jussi Pohjolainen
 
Android notification
Krazy Koder
 
Event Handling in java
Google
 
Introduction to Android Fragments
Sergi Martínez
 
Ad

Similar to Android ui menu (20)

PDF
Lecture-10-Menus.pdf of Mobile Application Development
MuhammadUsman359023
 
PPTX
Lecture 9: Menus and Additional Actions .pptx
Yousef Alamir
 
PPTX
Android Lab Test : Creating a menu dynamically (english)
Bruno Delb
 
PDF
01 10 - graphical user interface - others
Siva Kumar reddy Vasipally
 
PDF
Chapt 04 user interaction
Edi Faizal
 
PPTX
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Eng Teong Cheah
 
PPSX
Aula 6 - 08/05 (Menu)
Ricardo Longa
 
DOCX
Android resources in android-chapter9
Dr. Ramkumar Lakshminarayanan
 
PPTX
MAD_MENU286nvhvchvhmvjvjvmbvmbvmvbbm.pptx
himanshunanobhatt
 
PPTX
MenusAnddailogsinandroidddfdadusjsjdjdjs
omkardolas3
 
PDF
Android session 3
Ahesanali Suthar
 
PPTX
Android Development Training for Beginners - Activity
Joemarie Amparo
 
PPTX
Android Lab Test : Creating a menu context (english)
Bruno Delb
 
PPTX
Chapter 2 lesson-1 adding the action bar
Kalluri Vinay Reddy
 
PDF
The Action Bar: Front to Back
CommonsWare
 
PDF
Intake 38 8
Mahmoud Ouf
 
PDF
Intake 37 8
Mahmoud Ouf
 
PDF
Using android's action bar
Danilo Freitas de Souza
 
PDF
Android 4.0 UI design tips
Star Channel - Nea Tileorasi
 
PPTX
Menu in Android (Define,Create,Inflate and Click Handler)
Nabin Dhakal
 
Lecture-10-Menus.pdf of Mobile Application Development
MuhammadUsman359023
 
Lecture 9: Menus and Additional Actions .pptx
Yousef Alamir
 
Android Lab Test : Creating a menu dynamically (english)
Bruno Delb
 
01 10 - graphical user interface - others
Siva Kumar reddy Vasipally
 
Chapt 04 user interaction
Edi Faizal
 
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Eng Teong Cheah
 
Aula 6 - 08/05 (Menu)
Ricardo Longa
 
Android resources in android-chapter9
Dr. Ramkumar Lakshminarayanan
 
MAD_MENU286nvhvchvhmvjvjvmbvmbvmvbbm.pptx
himanshunanobhatt
 
MenusAnddailogsinandroidddfdadusjsjdjdjs
omkardolas3
 
Android session 3
Ahesanali Suthar
 
Android Development Training for Beginners - Activity
Joemarie Amparo
 
Android Lab Test : Creating a menu context (english)
Bruno Delb
 
Chapter 2 lesson-1 adding the action bar
Kalluri Vinay Reddy
 
The Action Bar: Front to Back
CommonsWare
 
Intake 38 8
Mahmoud Ouf
 
Intake 37 8
Mahmoud Ouf
 
Using android's action bar
Danilo Freitas de Souza
 
Android 4.0 UI design tips
Star Channel - Nea Tileorasi
 
Menu in Android (Define,Create,Inflate and Click Handler)
Nabin Dhakal
 
Ad

More from Krazy Koder (20)

PPT
2310 b xd
Krazy Koder
 
PPT
2310 b xd
Krazy Koder
 
PPT
2310 b xc
Krazy Koder
 
PPT
2310 b xb
Krazy Koder
 
PPT
2310 b 17
Krazy Koder
 
PPT
2310 b 16
Krazy Koder
 
PPT
2310 b 16
Krazy Koder
 
PPT
2310 b 15
Krazy Koder
 
PPT
2310 b 15
Krazy Koder
 
PPT
2310 b 14
Krazy Koder
 
PPT
2310 b 13
Krazy Koder
 
PPT
2310 b 12
Krazy Koder
 
PPT
2310 b 11
Krazy Koder
 
PPT
2310 b 10
Krazy Koder
 
PPT
2310 b 09
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 07
Krazy Koder
 
PPT
2310 b 06
Krazy Koder
 
2310 b xd
Krazy Koder
 
2310 b xd
Krazy Koder
 
2310 b xc
Krazy Koder
 
2310 b xb
Krazy Koder
 
2310 b 17
Krazy Koder
 
2310 b 16
Krazy Koder
 
2310 b 16
Krazy Koder
 
2310 b 15
Krazy Koder
 
2310 b 15
Krazy Koder
 
2310 b 14
Krazy Koder
 
2310 b 13
Krazy Koder
 
2310 b 12
Krazy Koder
 
2310 b 11
Krazy Koder
 
2310 b 10
Krazy Koder
 
2310 b 09
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 07
Krazy Koder
 
2310 b 06
Krazy Koder
 

Recently uploaded (20)

PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
infertility, types,causes, impact, and management
Ritu480198
 
Controller Request and Response in Odoo18
Celine George
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Difference between write and update in odoo 18
Celine George
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 

Android ui menu

  • 1. Android UI - Menus
  • 2. Topics • Types of menus • Options menu • Context menu • Submenu • Creating menu using Menu resource
  • 4. Types of Menus • Context menu > Floating list of menu items that may appear when you perform a long-press on a View • Options menu > Revealed when the device MENU key is pressed • Submenu > Used to organize menu items into groups > A Submenu item does not support nested Submenus
  • 6. Context Menu • Context menus do not support item shortcuts and item icons.
  • 7. How to Create Context Menu? • When Context menu is opened for the first time, the Android system will call the Activity's onCreateContextMenu(Menu menu) callback method. > Override this method in your Activity class and populate the Menu object given to you with MenuItem's. • You can populate the menu in two ways > Scheme #1: by calling add() for each item you'd like in the menu. > Scheme #2: by inflating a menu resource that was defined in XML (preferred)
  • 8. Populating Menu with Menu Items: #1 // Override this method of Activity class in order to create menu items. @Override public void onCreateContextMenu( ContextMenu menu, // Context menu that is being built View view, // The view for which the context menu is being built ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); menu.setHeaderTitle("Context menu"); menu.add(0, Menu.FIRST, Menu.NONE, "menu #1"); menu.add(0, Menu.FIRST + 1, Menu.NONE, "menu #2"); menu.add(0, Menu.FIRST + 2, Menu.NONE, "menu #3"); menu.add(0, Menu.FIRST + 3, Menu.NONE, "menu #4"); }
  • 9. How to handle Menu Selection? • When a menu item is selected from the Context Menu, onContextItemSelected() callback method of your Activity gets called > This callback passes you the MenuItem that has been selected. > You can identify the item by requesting the itemId, with getItemId(), which returns the integer that was assigned with the add() method. > Once you identify the menu item, you can take an appropriate action.
  • 10. Example: Handling Menu Selection /* Handles item selections */ public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_NEW_GAME: newGame(); return true; case MENU_QUIT: quit(); return true; } return false; }
  • 12. When to use Options Menu? • The Options Menu is where you should include basic application functions and any necessary navigation items (e.g., to a home screen or application settings).
  • 13. How Options Menu Work? • The Options Menu is opened by pressing the device MENU key. • When opened, the Icon Menu is displayed, which holds the first six menu items. • If more than six items are added to the Options Menu, then those that can't fit in the Icon Menu are revealed in the Expanded Menu, via the "More" menu item.
  • 14. Populating Menu with Menu Items: #1 /* Creates the menu items without Icons */ public boolean onCreateOptionsMenu(Menu menu) { // The add() method used in this sample takes four arguments: // groupId, itemId, order, and title. menu.add(0, MENU_NEW_GAME, 0, "New Game"); menu.add(0, MENU_QUIT, 0, "Quit"); return true; } /* Creates the menu items with Icons. Note that add() method returns newly created MenuItem object to set additional properties like an icon, a keyboard shortcut, an intent, and other settings for the item. */ public boolean onCreateOptionsMenu(Menu menu) { // The add() method used in this sample takes four arguments: // groupId, itemId, order, and title. menu.add(0, MENU_NEW_GAME, 0, "New Game").setIcon(R.drawable.menu_new_game_icon); menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon); return true; }
  • 15. How to handle Menu Selection? • When a menu item is selected from the Options Menu, onOptionsItemSelected() callback method of your Activity gets called > This callback passes you the MenuItem that has been selected. > You can identify the item by requesting the itemId, with getItemId(), which returns the integer that was assigned with the add() method. > Once you identify the menu item, you can take an appropriate action.
  • 16. Example: Handling Menu Selection /* Handles item selections */ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_NEW_GAME: newGame(); return true; case MENU_QUIT: quit(); return true; } return false; }
  • 18. When to use SubMenu? • If you have several menu items that can be grouped together with a title, consider organizing them into a Submenu.
  • 19. Example: Creating Submenu public boolean onCreateOptionsMenu(Menu menu) { boolean result = super.onCreateOptionsMenu(menu); // Create submenu "File" SubMenu fileMenu = menu.addSubMenu("File"); fileMenu.add("New"); fileMenu.add("Open File"); fileMenu.add("Close"); fileMenu.add("Close All"); // Create submenu "Edit" SubMenu editMenu = menu.addSubMenu("Edit"); editMenu.add("Undo Typing"); editMenu.add("Redo"); editMenu.add("Cut"); return result; }
  • 21. Why using Menu Resource? • Instead of instantiating Menu objects in your application code, you should define a menu and all its items in an XML menu resource, then inflate the menu resource (load it as a programmable object) in your application code. • Defining your menus in XML is a better practice (than creating them in code) because it separates your interface design from your application code (the same as when you define your Activity layout in XML).
  • 22. When to Use Menu Resource File? • Create <menu_resource>.xml under res/menu/ directory • Inflate the Menu Resource file using inflate(<menu-resource-id>) method of the MenuInflator class > Menu objects are created from the Menu resource file
  • 23. Example: Inflating Menu Resource public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu XML resource. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.title_only, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.jump: Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_LONG) .show(); return true; case R.id.dive: Toast.makeText(this, "Dive into the water!", Toast.LENGTH_LONG) .show(); return true; }
  • 24. Example: Menu Resource File <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/jump" android:title="Jump!" android:icon="@drawable/draw_jump" /> <item android:id="@+id/dive" android:title="Dive!" android:icon="@drawable/draw_dive" /> </menu>