Android menu implementation tutorial

16 06 2012

Here is shown how to make application menu under the ‘menu’ button. It looks like this one:

1. Configure menu items in xml file

Create xml file in res/menu. For example: main. xml. Put there items that will be in your menu. Mine will have just one item (settings), so my res/menu/main.xml looks like this:

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

Each menu item can laso have icon, that is set by this item attribute:


2. Open menu after ‘menu’ button click

Once you already have layout, now override method that will show it to the user after ‘menu’ button click. To do so, in activity, you want to show menu, override the method:

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

Now menu will appear after button touch. It’s time to implement menu items behaviour.

3. Implement action called after menu item selection

To handle menu item click, you need to override in your activity the method:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
       	       runNewActivity(SettingsActivity.class, null);
    return true;

Here, I am starting new activity with application settings after item click. Although my menu has just one item, I use switch statement to show how to handle more items. To do so, one have just to add another cases switching over menu items ids, specified in menu.xml.

4. How to show the same menu in all application activities?

Not by copy-pasting code to all activities for sure! :)

Create dedicated Activity (for example MyAppActivity) extending android Activity, where you will put methods from this tutorial. Activity does not have to do anything else.

Then make all of your application Activities override MyAppActivity, instead of standard Android Activity:

public class LoadActivity extends MyAppActivity

Moreover here is some another good tutorial on creating app menu and preferences screen.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: