Sie sind auf Seite 1von 24

Assignment

Mobile Application Development


(CSI-601)
BS-CS , Section: A

GC Women University, Fsd.


7th Semester

Topic: Android Menus


Submitted to: Ms. Sadia Sahar
Group #: 4
Submitted by: Mehwish Mustafa (15-107), Rabiya Sadaqat (15-108),
Maham Asghar (15-127), Sana Naz (15-172)
ANDROID MENUS
How to create menus in Android?
What is a Menu in Android?

■ A list of possible available options to choose from.


■ Common UI component in many applications.
■ Provides a familiar and consistent user experience.
■ Three types:
– Options Menu (Primary)
– PopUp Menu (Secondary or Sub-Menu)
– Context Menu
Types of Menus

1. Options Menu.
2. Context Menu.
3. Contextual action bar.
4. Popup Menu.
Uses of Menus

■ Easier to visualize the menu structure in XML.


■ Seperates the content for the menu.
■ Allows to create alternative menu configurations for different platform versions,
screen sizes.
■ Takes minimum space on screen.
Options Menu

■ It is the “Primary Menu” of any application.


■ Mainly used for including actions/options that are relevant to current activity’s
context such as “Search”, “Settings” and “Preferences”.
■ It is represented by an Overflow Button (Three Vertical Dots).
■ By Default it’s position is on the top-right corner of app/action bar.
■ Previous APIs allowed only six items to be appeared in options menu.
■ Latest APIs do not have any restriction upon number of items in options menu.
■ It is created outside the onCreate() function but inside the main class.
How to create an Options Menu?

■ Steps to create an options menu:


1) Open an activity class.
2) Create a resources folder.
3) Create a menu XML file.
4) Add items to your menu. (Compulsory attribute is “title” along with “id”).
5) Inflate your Menu Resource.
6) Detect user Interaction.
7) Respond to Menu Item Selection.
Creating Menu Resource Directory & File

Step 1. Start New Project Step 2. Create Menu Resource Directory Step 3. Only change “Resource Type” to menu

Step 4. Click ok Step 5. Create menu resource file in menu resource Step 6. Name the file and click ok
Adding Items To (Options) Menu Resource

Step 1. Open the menu resource file recently created Step 2. Add desired items in the menu resource.
“optional.xml”
Inflate Menu Resource
■ Open Java file (Backend File of activity).
■ Place the cursor outside the body of “onCreate()” and add “onCreateOptionsMenu()”
function.

Step 1. Create “onCreateOptionsMenu()” function (outside


onCreate())
Responding to Menu Item Selection

■ “onOptionsItemSelected()” function is used to perform action on single item.


(outside onCreate()).

Step 1. Create “onOptionsItemSelected()” function (outside


onCreate())
Cont.

■ onCreateOptionsMenu(): This method is used to specify the options menu for an


activity. Menu resource file (defined in XML) can be inflated into menu.
■ onOptionsItemSelected(): This method is called, when an item in the options menu
is selected. It specifies some action that is to be performed whenever an item is
clicked.
■ getMenuInflater(): It is used to instantiate menu XML files into Menu objects.
■ .inflate(R.menu.optional,menu): It inflates a menu hierarchy from the specified XML
resource.
■ menu: The menu to inflate into. The items and submenus will be added to this
menu.
Pop Up Menu

■ Also called “Secondary Menu”.


■ Displays a list of items in a modal popup window.
■ Mostly used for showing subitems, where extra items are required.
■ Popup menu is applied on any widget/view.
■ It will appear below the view if there is a room or above the view in case if there is no
space
■ It will be closed automatically when we touch outside of the popup.
■ The actions in popup menu won’t affect the corresponding content.
■ Popup menu created inside onCreate() function.
How to create a PopUp Menu?

■ Steps to create a popup menu:


1) Open an activity class.
2) Create a resources folder.
3) Create a menu XML file.
4) Add items to your menu. (Compulsory attribute is “title” along with “id”).
5) Add onClick Listener.
6) Inflate your Menu Resource.
7) Detect user Interaction.
8) Respond to Menu Item Selection.
Adding Items to (Popup) Menu Resource
■ For creating popup menu resource file, repeat the steps shown on slide 8. Here
“popup.xml” is created.
■ Then add (desired) items.

Step 1. Create “onCreateOptionsMenu()” function (outside


onCreate())
Inflate Menu Resource
■ A class “PopupMenu” is used to create popup menus.
■ Use this class inside setOnClickListener().
■ Syntax: PopupMenu object=new PopupMenu (context,widget).
Responding to Menu Item Selection

■ “onMenuItemClickListener()” function is used to perform action on single item.


(inside onCreate()).

Step 1. Create “object.onMenuItemClickListener()” function


(inside onCreate())
Context Menu

■ Android context menu appears when user press long click on the element.
■ It is also known as floating menu.
■ It is useful to implement an actions that effect the selected content or context
frame.
■ It doesn't support item shortcuts and icons.
■ The android Context Menu is more like the menu which displayed on right click in
Windows or Linux.
■ Implementing the context menu is straightforward, and is a key ingredient in many
applications.
How to create an Context Menu?

■ Steps to create an Context menu:


1) Create an XML Activity..
2) Create a menu resource file (XML file) in menu folder .
3) Add items to your menu. (Compulsory attribute is “title” along with “id”).
4) Register your view to apply context menu.
5) Inflate your Menu Resource.
6) Detect user Interaction.
7) Respond to Menu Item Selection.
Creating Menu Resource File & Adding Items

Step 1. Create menu resource file in menu resource Step 2. Name the file and click ok Step 3. Add desired items in the menu
resource.
Register View for Context menu
■ Open Java file (Backend File of activity).
■ Create Object of any view(to which the menu is to be applied) and assign id.
■ “registerForContextMenu(View v)” function is used to register specific view for
context menu.(inside OnCreate())

Step 1. Register view for Context Menu (outside onCreate())


Inflate Menu Resource
■ Place the cursor outside the body of “onCreate()” and add “onCreateContextMenu()”
function.

Step 1. Create “onCreateContextMenu()” function (outside


onCreate())
Responding to Menu Item Selection

■ “onOnContextItemSelected()” function is used to perform action on single item.


(outside onCreate()).

Step 2. Create “onContextItemSelected()” function (outside


onCreate())
Cont.

■ onCreateContextMenu(): This method is used to specify the Context menu for a view.
Menu resource file (defined in XML) can be inflated into menu
– menu: Context menu : the Contect menu that is being built.
– v : View: view for which the context menu is being built.
– menuInfo: ContextMenu.ContextMenuInfo:Extra information about the item for
which the context menu should be shown.
■ menu.setHeaderTitle(): This method is used to set the header of context menu.
■ onContextItemSelected(): This method is called, when an item in the options menu is
selected. It specifies some action that is to be performed whenever an item is
clicked.

Das könnte Ihnen auch gefallen