Professional Documents
Culture Documents
Beginning with Android 3.0 (API level 11), Android-powered devices are no longer
required to provide a dedicated Menu button. With this change, Android apps should
migrate away from a dependence on the traditional 6-item menu panel and instead
provide an app bar to present common user actions.
The options menu is the primary collection of menu items for an activity. It's
where you should place actions that have a global impact on the app, such as
"Search," "Compose email," and "Settings."
3. Popup menu
2. It separates the content for the menu from your application's behavioral code.
3. It allows you to create alternative menu configurations for different platform versions, screen sizes,
and other configurations by leveraging the app resources framework.
A new menu directory would be made under res directory. Add menu_file.xml file in menu directory by right
clicking on menu --> New --> Menu resource file.
To define the menu, create an XML file inside your android:title
projects res/menu/ directory and build the menu with A reference to a string to use as the item's
the following elements: title.
android:showAsAction
<menu> Specifies when and how this item should
Defines a Menu, which is a container for appear as an action item in the app bar.
menu items. A <menu> element must be the root
node for the file and can hold one or You can add a submenu to an item in any
more <item> and <group> elements. menu by adding a <menu> element as the child of
an <item>.
<item>
Creates a MenuItem, which represents a Submenus are useful when your
single item in a menu. This element may contain a application has a lot of functions that can be
nested <menu>element in order to create a submenu. organized into topics, like items in a PC
application's menu bar (File, Edit, View, etc.).
<group>
An optional, invisible container For example:
for <item> elements. It allows you to categorize
menu items so they share properties such as active <?xml version="1.0" encoding="utf-8"?>
state and visibility. For more information, see the
<menu
section about Creating Menu Groups.
xmlns:android="http://schemas.android.com/apk/res/
Here's an example menu named game_menu.xml: android">
<item android:id="@+id/file"
<?xml version="1.0" encoding="utf-8"?> android:title="@string/file" >
<menu <!-- "file" submenu -->
xmlns:android="http://schemas.android.com/apk/re <menu>
s/android"> <item android:id="@+id/create_new"
<item android:id="@+id/new_game" android:title="@string/create_new" />
android:icon="@drawable/ic_new_game" <item android:id="@+id/open"
android:title="@string/new_game" android:title="@string/open" />
android:showAsAction="ifRoom"/>
</menu>
<item android:id="@+id/help"
</item>
android:icon="@drawable/ic_help"
android:title="@string/help" /> </menu>
</menu>
<item android:id="@+id/menu_profile"
android:title="Profile" />
<item android:id="@+id/setting"
android:title="Setting" />
<item android:id="@+id/account"
android:title="Account" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
When you want to action perform on click menu item you have to use public
boolean onOptionsItemSelected(MenuItem item) method.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.search:
// implement your code
Toast.makeText(getApplicationContext(),"Search clicked",Toast.LENGTH_LONG).show();
break;
case R.id.profile:
// implement your code
Toast.makeText(getApplicationContext(),"Profile clicked",Toast.LENGTH_LONG).show();
break;
case R.id.setting:
// implement your code
Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();
break;
case R.id.account:
// implement your code
Toast.makeText(getApplicationContext(),"Account clicked",Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
3. Implement onContextItemSelected()
Here you can code for click menu item.The getItemId() method queries the ID for the selected menu item,
which you should assign to each menu item in XML using the android:id attribute.
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.rename:
// implement your code
Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();
return true;
case R.id.change_background:
// implement your code
Toast.makeText(getApplicationContext(),"change background",Toast.LENGTH_LONG).show();
return true;
case R.id.setting:
// implement your code
Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();
return true;
case R.id.account:
// implement your code
Toast.makeText(getApplicationContext(),"Account clicked",Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
popupmenu.setOnClickListener (new View.OnClickListener ()
{
@Override
public void onClick(View v) {
showPopupMenu(v);
}
});
@Override
public boolean onMenuItemClick(MenuItem item)
{
switch (item.getItemId()) {
case R.id.delete:
case R.id.save:
//implement your code
Toast.makeText(MainActivity.this,"save clicked",Toast.LENGTH_LONG).show();
break;
default:
break;
};
return true;
}
});
popupMenu.show();
}