Tuesday, December 17, 2013

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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9rALoomLDCnfvJkPBpKjWDz-wyknfX2aAmumU3vizGDg5YaLJQKqHrGi6Br3FVvZ31YtjUbrDMpNZ4XVo3Y2RiX1F1fYo6y8pQekwMQQWe2BceyK9Z8ABAySd-CHap2pKiXiL5fu3KSs/


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQTSMKYEnY1K2VrrE3J5NFfwDz1ezPsmcHSVTuBg8ibrqEE1wni6nNtczKnM8aHO0ZzEfgyu21-4GxAPrPKP6CIqyxDV1GqVUQcmZQhF3Gk2B_rdNTH6VUzn26HI54bibtD9D0mqeYngc/