In android application menu is one of the important user interface entity which provides some action options for a particular view. In this tutorial i am discussing about creating android menus.

Download Code

The final output of menu tutorial will be like below image

Menus Output

In this tutorial we are creating a simple menu with 6 menu items. On clicking on single menu item a simple Toast message will be shown.

1. Create a new project File ⇒ New ⇒ Android Project and give activity name as AndroidMenusActivity.
2. Now create an XML file under res/layout folder and name it as menu.xml.
3. Open menu.xml file and type following code. In the following code we are creating a single menu with 6 menu items. Each menu item has an icon and title for display the label under menu icon. Also we have id for each menu item to identify uniquely.

4. Now open your main Activity class file (AndroidMenusActivity.java) and type following code. In the following code each menu item is identified by its ID in switch case statement.

package com.androidhive.androidmenus;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    // Initiating Menu XML file (menu.xml)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        
        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
        	// Single menu item is selected do something
        	// Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_save:
        	Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_search:
        	Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_share:
        	Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_delete:
        	Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_preferences:
        	Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }    

}

5. Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application to test your application. On Android Emulator click on Menu Button to launch menu.

launch menu
Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
9 years ago

[…] Share i Exit Chciałem się dowiedzieć co zrobić aby do takiego menu: How to create Android Menus Tutorial dodać te opcje które podałem w tytule, jeśli ktoś mógł odpowiedzieć […]

Prashant Kolhe
Prashant Kolhe
3 years ago

How to show menu bar on long press on button or list view item otherwise hide the menu bar

Ravi Tamada
3 years ago
Reply to  Prashant Kolhe

You mean to show Appbar?

Sanjay Panchal
Sanjay Panchal
3 years ago

instead of -> (R.layout.menu, menu);
write -> (R.menu.menu, menu);

pankaj kumar
pankaj kumar
3 years ago

How to create PopupMenu in android having two buttons in a activity and show different different popup option on each button….how it can be done..

5
0
Would love your thoughts, please comment.x
()
x