Android –
Menu –
Menu is an efficient and friendly way to show multiple options, from which users can choose what they like.
Android have the following three type of menus:
- Option Menu
- Context Menu
- Popup Menu
Steps:
Defining a Menu in xml:
We can define the menu, create an XML file inside your project’s res/menu/directory and build the menu with the following elements.
<menu>
Menu is a container for menu items. A <menu> element must be the root node for the file and hold one or more <item> and <group>elements.
<item>
We have to create a MenuItem, which represents a single item in a menu. Item element contains a nested <menu> element in order to create a submenu.
Here is an example of game_menu.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:id=”@+id/new_game” android:title=”New Game” android:icon=@drawable/ic_new_game
android:showASAction=”ifRoom”/>
<item android:id=”@+id/help” android:title=”Item2″
android:icon=@drawable/ic_help
android:title=”Help”
/>
</menu>
The <item> element supports several attributes you can use to define an item’s appearance and behavior.
For Free, Demo classes Call: 8037516148
Registration Link: Click Here!
Adding a submenu(Nested Menu):
To add a submenu to an item in any menu (except a submenu) you have to add <menu> element as the child of an <item>.
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:id=”@+id/file”
android:title=”File”>
<menu>
<item android:id=”@+id/create_new” android:title=”Create New”/>
<item android:id=”@+id/open” android:title=”Open”/>
</menu>
</item>
</menu>
How to use the menu in your Activity?
To use the menu in your activity, you need to inflate the menu resource (convert hte XML resource into a programmable object) using MenuInflater.inflate().
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.game_menu,menu;
Defining action on Menu Click:
Public boolean onOptionsItemSelected(MenuItem item)
{
Return super.onOptionsItemSelected(item);
}
Android option Menu:
The options menu is the primary collection of menu items for an activity.
Its where you should place actions that have a global impact on the app, such as “Search”, “Compose email”, and “Settings”.
For Free, Demo classes Call: 8037516148
Registration Link: Click Here!
Steps of creating Option Menu:
- Define menu items in res/menu.xml
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:id=”@+id/item1″ android:title=”Item1″/>
<item android:id=”@+id/item2″ android:title=”Item2″/>
<item android:id=”@+id/item3″ android:title=”Item3″/>
</menu>
In Activity Class:
2.Override onCreateOptionsMenu()
It is this method where you inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.options_menu,menu);
return true;
}
Define Click Events:
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(getApplicationContext(),“Item 1 is selected”,Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),“Item 2 is selected”,Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),“Item 3 is selected”,Toast.LENGTH_SHORT).show();
return true;
default:return super.onOptionsItemSelected(item);
}
}
Note:
- Items for the options menu can be declared from either your Activity subclass or a Fragment subclass.
- If both , activity and fragment(s) declare items for the options menu, thay are combined in the UI.
- The activity’s items appear first, followed by those of each fragment in the order in which each fragment is added to the activity.
For Free, Demo classes Call: 8037516148
Registration Link: Click Here!
Android Context Menu:
Android Context Menu appears when a user long clicks on the item/element.
It is also known as a floating menu.
- Define menu items in res/menu.xml
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:id=”@+id/item1″ android:title=”Item1″/>
<item android:id=”@+id/item2″ android:title=”Item2″/>
<item android:id=”@+id/item3″ android:title=”Item3″/>
</menu>
- In Activity class:
Implement onContextItemSelected()
@Override
public boolean onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
getMenuInflater().inflate(R.menu.context_menu,menu);
return true;
}
- Define Click Events:
When the user selects a menu item, the system calls onContextItemSelected() methods so you can perform the appropriate action for example:
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()==“Delete”)
Toast.makeText(getApplicationContext(),“Delete was Pressed”,Toast.LENGTH_SHORT).show();
else
if(item.getTitle()==“UpperCase”)
Toast.makeText(getApplicationContext(),“UpperCase was Pressed”,Toast.LENGTH_SHORT).show();
else
if(item.getTitle()==“LowerCase”)
Toast.makeText(getApplicationContext(),“LowerCase was Pressed”,Toast.LENGTH_SHORT).show();
return true;
}
Android Popup Menu:
Android popup Menu displays the menu below the another text if space is available otherwise above the anchor text.
Define menu items in res/menu.xml
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:id=”@+id/item1″ android:title=”Item1″/>
<item android:id=”@+id/item2″ android:title=”Item2″/>
<item android:id=”@+id/item3″ android:title=”Item3″/>
</menu>
- If you define your menu in XML,here’s how you can show the popup menu.
E.g. A button with the android.onClick attribute that shows a popup menu:
<ImageButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android.src=”@drawable/ic_overflow_holo_dark”
android.onClick=”showPopup”
android.contentdescription=”@string/descr_overflow_button”/>
For Free, Demo classes Call: 8037516148
Registration Link: Click Here!
The activity can then show the popup menu like this:
public void showPopup(View v){
PopupMenu popup=new PopupMenu(this,v);
getMenuInflater().inflate(R.menu.actions,popup.getMenu());
popup.show();
}
Define click events:
@Override
public boolean onMenuItemClick(MenuItem item){
switch (item.getItemId()){
case R.id.active:
Toast.makeText(getApplicationContext(),“Item 1 is selected”,Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(getApplicationContext(),“Item 2 is selected”,Toast.LENGTH_SHORT).show();
return true;
default:return false;
}
}
Author:-
Jyostna Binjwe
SevenMentor Pvt Ltd
Call the Trainer and Book your free demo Class for now!!!
call icon
© Copyright 202! | Sevenmentor Pvt Ltd.