Android Simple Menu Example - ANDROID - Helper

Monday, April 25, 2011

Android Simple Menu Example

SIMPLE MENU

SOURCE CODE is

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;
               
                @Override
                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);
                }
   
                @Override
                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,
                                "Close").setIcon(R.drawable.icon);

                                return (super.onCreateOptionsMenu(menu));

                }

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

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


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



The OUTPUT will be


https://lh6.googleusercontent.com/_xnrF0YSUul8/TbU9bmmCPnI/AAAAAAAAAHo/TBiBEFln8tY/simple_menu1.png


https://lh6.googleusercontent.com/_xnrF0YSUul8/TbU9bcLfk4I/AAAAAAAAAHk/aO8NUtKg3Cw/simple_menu2.png

10 comments:

  1. Wow , Its is Very useful and nice blog

    ReplyDelete
  2. 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!

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

    ReplyDelete
  4. 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?

    ReplyDelete
  5. Error icon ok, no declare encapsule type.

    ReplyDelete
  6. Ok. 2name iquals.

    ReplyDelete
  7. 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.

    ReplyDelete