Working With Menu
Working With Menu
• Android menus - creating menus, working with menu groups, responding to menu
items,
• Icon menu, sub menu, context menu, dynamic menus, loading menu through
XML, popup menus
• Fragments in Android ,structure of fragment, fragment life cycle, fragment
transaction and back stack, fragment manager, saving fragment state, persistence
of fragments, communications with fragments ,startActivity() and
setTargetFragment()
• Using dialogs in android, dialog fragments
• Working with toast, Implementing action bar - tabbed navigation action bar
activity,
• Implementing base activity classes, tabbed action bar and tabbed listener, debug
text view layout
• action bar and menu interaction, list navigation action bar activity
• spinner adapter, list listener, list action bar, standard navigation action bar activity
• action bar and search view, action bar and fragments
Menu in Android
➢Menus are a common user interface component in many types of
applications
➢The key class in Android menu support is android.view.Menu.
➢Menu items are represented by android.view.MenuItem.
➢Submenus are represented by android.view.SubMenu
➢A Menu object contains a set of menu items.
➢ A menu item carries the following attributes:
➢ Name: A string title
➢ Menu item ID: An integer
➢ Group ID: You can group menu items together by assigning each one a
group ID. Multiple menu items that carry the same group ID are considered
part of the same group.
➢ Sort order: An integer identifying the order of this menu item when it is
displayed in the menu
➢There are 3 types of menus in Android:
➢Option Menu: The options menu is the primary collection of menu
items for an activity.
➢ It's where you should place actions that have an overall impact on
the app, such as Search, Compose Email and Settings.
➢Context Menu : A context menu is a floating menu that appears
when the user performs a long-click on an element.
➢It provides actions that affect the selected content or context frame.
➢Pop-up Menu : A popup menu displays a list of items in a vertical
list that's anchored to the view that invoked the menu
Defining a Menu in XML
➢For all menu types, Android provides a standard XML format to define menu
items.
➢ Instead of building a menu in activity's code, define a menu and all its items in an
XML menu resource and then inflate the menu resource (load it as a Menu object) in
activity or fragment.
➢Using a menu resource is a good practice for a few reasons:
➢It's easier to visualize the menu structure in XML.
➢It separates the content for the menu from application's behavioral code.
➢It allows to create alternative menu configurations for different platform
versions, screen sizes, and other configurations by leveraging the app resources
framework.
To define the menu, create an XML file inside project's res/menu/ directory and
build the menu_file.xml with the following elements
◦ <menu>:-Defines a Menu, which is a container for menu items. A <menu>
element must be the root node for the file and can hold one or more <item> and
<group> elements.
◦ <item>:Creates a MenuItem, which represents a single item in a menu. This
element may contain a nested <menu> element in order to create a submenu.
◦ <group>:An optional, invisible container for <item> elements. It allows to
categorize menu items so they share properties such as active state and
visibility.
➢<item> element supports several attributes
➢android:id: A resource ID that's unique to the item, which allows the
application to recognize the item when the user selects it.
➢android:icon: A reference to a drawable to use as the item's icon.
➢android:title: A reference to a string to use as the item's title.
➢android:showAsAction: Specifies when and how this item should appear
as an action item in the app bar.
➢To use the menu in your activity, you need to inflate the menu resource
(convert the XML resource into a programmable object) using MenuInflater.inflate()
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:title="@string/file“/ >
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
Creating an Option Menu
To make an option menu, we need to override onCreateOptionsMenu() method
as follows:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file, menu);
return true;
}
MenuInflater inflater = getMenuInflater();
This gives a menuInflater object that will be used to inflate(convert our xml file
into java object) the menu_file.Xml file.
inflater.inflate(R.menu.menu_file, menu);
inflate() method is used to inflate the menu_file.Xml file.
Option menu
Handling Click Events
When the user selects an item from the options menu, the system calls
activity's onOptionsItemSelected() method.
This method passes the MenuItem selected.
The item can be identified by calling getItemId() method, which returns the
unique ID for the menu item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle item selection
switch (item.getItemId()) {
case R.id.i1:
//perform any action;
return true;
case R.id.a:
//perform any action;
return true;
default:
return super.onOptionsItemSelected(item); }
}
Creating Context Menu
➢Register The View To Which The Context Menu Should Be Associated By
Calling RegisterForContextMenu() And Pass It The View.
➢Implement The OnCreateContextMenu() Method In Your Activity. :When The
Registered View Receives A Long-click Event, The System Calls Your
OnCreateContextMenu() Method
@override
public void onCreateContextMenu(contextMenu menu, View v,
contextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file, menu); }
When the user selects a menu item, the system calls onContextItemSelected()
Creating Popup Menu
➢Make an object of PopupMenu, whose constructor takes the current application
Context and the View to which the menu should be anchored.
➢Use MenuInflater to inflate your menu resource into the Menu object returned
by PopupMenu.getMenu()
➢Call PopupMenu.show()
➢A button that shows a popup menu when clicked on it:
➢<Button android:id="@+id/button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"
android:onClick="pop" />
Creating Popup Menu
The activity can then show the popup menu
public void pop(View v){
PopupMenu popup = new PopupMenu(this,v);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file,popup.getMenu());
popup.show();
}
▪To perform an action when the user selects a menu item,implement the
PopupMenu.OnMenuItemClickListener interface to Activity and register it
with PopupMenu by calling setOnMenuItemclickListener().
▪When the user selects an item, the system calls the onMenuItemClick()
method in the Activity.
Working with Menu Groups
A menugroup is a collection of menu items that share certain characteristics
A group, can:
◦ Show or hide all items with setGroupVisible()
◦ Enable or disable all items with setGroupEnabled()
◦ Specify whether all items are checkable with setGroupCheckable()
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_save“
android:icon="@drawable/ menu_save " >
android:title="@string/menu_Save" >
<group android:id=“@+id/group_delete”
<item android:id="@+id/menu_archieve"
android:title="@string/ menu_archieve " />
<item android:id="@+id/menu_delete"
android:title="@string/ menu_delete " />
</group> </menu>