Android - Menu

Card Puncher Data Processing


What is a menu (or toolbar)?

XML Example

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
    <item android:id="@+id/new_game"
    <item android:id="@+id/help"
          android:title="@string/help" />


Java Inflate

If both your activity and fragment(s) declare items for the options menu, they 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. If necessary, you can re-order the menu items with the android:orderInCategory attribute in each <item> you need to move.


To specify the options menu for an activity, override onCreateOptionsMenu()

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(, menu);
    return true;


public void onCreate(Bundle savedInstanceState) {
	setHasOptionsMenu(true); // Report that this fragment has a menu and that therefore the onCreateOptionsMenu(Menu, MenuInflater) and related methods will be called back.

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
	inflater.inflate(, menu);

public boolean onOptionsItemSelected(MenuItem item) {
	int id = item.getItemId();
	if (id == {
		// Do what you want
		return true;
	return super.onOptionsItemSelected(item);

Documentation / Reference

Discover More
Card Puncher Data Processing
Android - (App) Resources (R)

The /res subdirectories contain the resources for your application: drawable-/: Directories for drawable resources, other than launcher icons, designed for various densities. layout: Directory...

Share this page:
Follow us:
Task Runner