Mobile Computing – 650003
MCA – ISTAR, V.V. Nagar
1Mr. Pritesh N. Patel (ISTAR)
Menu
 Menus are the additional options that are displayed
by clicking menu button of mobile.
 It is used to display thos...
How to create Options Menu
 To show option menu when user click on MENU
button on device we have to override the
implemen...
Syntax
public boolean onCreateOptionsMenu(android.view.
Menu menu)
{
super. onCreateOptionsMenu(menu);
…..
return true;
}
...
Sample Code Example - 1
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
super. onCreateOptionsMenu(menu);
men...
Example -2
 Use xml file to declare menu options
 Path res/menu/my_menu.xml
Sample code of xml file
<?xml version="1.0" ...
Code for event Example - 2
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflat...
Perform Action on Menu Item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
ca...
Creating group Menus
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/androi...
Creating Sub Menus
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"...
More about menu
public boolean onCreateOptionsMenu(Menu menu)
{
super. onCreateOptionsMenu(menu);
MenuItem m1 = menu.add(0...
Context Menu
 It is a type of Menu that is displayed when a long press
is performed on any view or control.
 It is used ...
Syntax
@Override
public boolean onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuinfo)
{
super.onCreateCo...
Sample Code
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreate...
Define context menu for Button
Button b = (Button) findViewById(R.id.btnsearch);
registerForContextMenu(b);
We can also us...
Upcoming SlideShare
Loading in...5
×

Android menus

2,415

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,415
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
46
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android menus

  1. 1. Mobile Computing – 650003 MCA – ISTAR, V.V. Nagar 1Mr. Pritesh N. Patel (ISTAR)
  2. 2. Menu  Menus are the additional options that are displayed by clicking menu button of mobile.  It is used to display those options that are not directly visible on the main UI of an application.  Types of Menus  Options Menu – Activated by clicking MENU key  Context Menu – Activated by long press on particular item. ( Same as right click in computer on screen )  Sub Menu – Do not support icon or nested sub menu 2Mr. Pritesh N. Patel (ISTAR)
  3. 3. How to create Options Menu  To show option menu when user click on MENU button on device we have to override the implementation of onCreateOptionsMenu() in Activity class of our application.  The options menu may contain icons, submenus and keyboard shortcuts.  It is used to provide additional options for navigation or configuration.  To handle menu click or press event we implements onOptionsItemSelected() method of activity. 3Mr. Pritesh N. Patel (ISTAR)
  4. 4. Syntax public boolean onCreateOptionsMenu(android.view. Menu menu) { super. onCreateOptionsMenu(menu); ….. return true; } public boolean onOptionsItemSelected(android.view. Menu menu) { ….. return true; } 4Mr. Pritesh N. Patel (ISTAR)
  5. 5. Sample Code Example - 1 public boolean onCreateOptionsMenu(android.view.Menu menu) { super. onCreateOptionsMenu(menu); menu.add(“File”) .setIcon(android.R.drawable.ic_iconfile) .setIntent(new Intent(this,FormActivity.class)); menu.add(“Edit”) .setIcon(android.R.drawable.ic_iconedit) .setIntent(new Intent(this,FormActivity.class)); return true; } Note: No need to define onOptionsItemSelected() event 5Mr. Pritesh N. Patel (ISTAR)
  6. 6. Example -2  Use xml file to declare menu options  Path res/menu/my_menu.xml Sample code of xml file <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu> 6Mr. Pritesh N. Patel (ISTAR)
  7. 7. Code for event Example - 2 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } The getMenuInflater() method returns a MenuInflater for the activity. With this object, you can call inflate(), which inflates a menu resource into a Menu object. In this example, the menu resource defined by game_menu.xml is inflated into the Menu that was passed into onCreateOptionsMenu(). 7Mr. Pritesh N. Patel (ISTAR)
  8. 8. Perform Action on Menu Item @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.new_game: Toast.makeText(getBaseContext(), "U click new", Toast.LENGTH_SHORT).show(); return true; case R.id.help: //showHelp(); return true; default: return super.onOptionsItemSelected(item); } } 8Mr. Pritesh N. Patel (ISTAR)
  9. 9. Creating group Menus <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:icon="@drawable/item1" android:title="@string/item1" /> <!-- menu group --> <group android:id="@+id/group1"> <item android:id="@+id/groupItem1" android:title="@string/groupItem1" /> <item android:id="@+id/groupItem2" android:title="@string/groupItem2" /> </group> </menu> 9Mr. Pritesh N. Patel (ISTAR)
  10. 10. Creating Sub Menus <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:icon="@drawable/item1" android:title="@string/item1" /> <item android:id="@+id/item2" android:icon="@drawable/item1" android:title="@string/item2" > <!– sub menu --> <menu> <item android:id="@+id/subitem1"> android:title=“Sub Item 1" /> <item android:id="@+id/subitem2" android:title=“Sub Item 2" /> </menu> </item> </menu> 10Mr. Pritesh N. Patel (ISTAR)
  11. 11. More about menu public boolean onCreateOptionsMenu(Menu menu) { super. onCreateOptionsMenu(menu); MenuItem m1 = menu.add(0,0,0,”Item1”); { m1.setAlphabeticShortcut(‘a’); m1.setIcon(R.drawable.icon); } //add as many you wish … return true; } Syntax: menu.add(groupID, itemId, order, title ) 11Mr. Pritesh N. Patel (ISTAR)
  12. 12. Context Menu  It is a type of Menu that is displayed when a long press is performed on any view or control.  It is used to perform some additional operation or action.  ContextMenu object is more complex then OptionsMenu object to implement.  We need to implement onCreateContextMenu() event of an activity but before that we must call registerForContextMenu() method and pass a view for which we want a context menu.  Different view can have different context menu or same. 12Mr. Pritesh N. Patel (ISTAR)
  13. 13. Syntax @Override public boolean onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuinfo) { super.onCreateContextMenu(menu, v, menuinfo); ………. return true; } @Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); } 13Mr. Pritesh N. Patel (ISTAR)
  14. 14. Sample Code @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.edit: editNote(info.id); return true; case R.id.delete: deleteNote(info.id); return true; default: return super.onContextItemSelected(item); } } 14Mr. Pritesh N. Patel (ISTAR)
  15. 15. Define context menu for Button Button b = (Button) findViewById(R.id.btnsearch); registerForContextMenu(b); We can also use b.setOnCreateContextMenuListener(this); To register context menu for button or any control. 15Mr. Pritesh N. Patel (ISTAR)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×