• Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 10 comments



package com.MenuExample;

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

public class MenuExample extends Activity
private static final int MENU_ITEM_1 = Menu.FIRST + 1;
                private static final int MENU_ITEM_2 = Menu.FIRST + 2;
                private static final int MENU_ITEM_3 = Menu.FIRST + 3;
                Toast msg;
                public void onCreate(Bundle savedInstanceState)
                public boolean onCreateOptionsMenu(Menu menu)
                                menu.add(Menu.NONE, MENU_ITEM_1, Menu.NONE,
                                "Menu Item 1").setIcon(R.drawable.icon);

                                menu.add(Menu.NONE, MENU_ITEM_2, Menu.NONE,
                                "Menu Item 2").setIcon(R.drawable.icon);

                                menu.add(Menu.NONE, MENU_ITEM_3, Menu.NONE,

                                return (super.onCreateOptionsMenu(menu));


                public boolean onOptionsItemSelected(MenuItem item)
                                switch (item.getItemId())
case MENU_ITEM_1:
                                                                 msg = Toast.makeText(MenuExample.this,
                                                                                "Menu Item 1 Clicked", Toast.LENGTH_LONG);

                                                case MENU_ITEM_2:
                                                                 msg = Toast.makeText(MenuExample.this,
                                                                                "Menu Item 2 Clicked", Toast.LENGTH_LONG);

                                                case MENU_ITEM_3:
                                return (super.onOptionsItemSelected(item));

The OUTPUT will be



10 Responses so far.

  1. Thanappan says:

    Wow , Its is Very useful and nice blog

  2. nice so simple.. thanks

  3. bacbim says:

    hello jack! i don't understand what was happend.I copied your code and then launch android emulator.but I can't see anything.OptionMenu can't show on screen.I tried do ways on forum as android-er.blogspot.com and android-coding.blogspot.finally, I still can't do it.help me.thanks a lot!

  4. this is a very helpful but also attach XML file code and damo

  5. arya says:

    nice It's working

  6. it need to attach .xml File

  7. LucidBrot says:

    Hi there,
    Nice, somewhat elegant code, but there remains a question:
    Does it run itself?
    Probably not, so how do I all it from another activity? or is ist automagically called by pressing the menu button on the phone?

  8. Anonymous says:

    Error icon ok, no declare encapsule type.

  9. Anonymous says:

    Ok. 2name iquals.

  10. now, i want to have two button and when i click a button, it will show a menu, and when i click a other button, it will show a other menu, how will i do ? please, let's help me.

Leave a Reply