• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Day   3 android menus - 14092012

Day 3 android menus - 14092012






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Day   3 android menus - 14092012 Day 3 android menus - 14092012 Presentation Transcript

    • Mr. Pritesh N. Patel Assistant Professor MCA STAR, 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 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)
    • 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)
    • 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) This event is used to create option menu This event is used to perform all action when any option menu is selected
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • 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)
    • Mr. Pritesh N. Patel (ISTAR) 16