Monday, October 10, 2011

Android Option Menu Example

SIMPLE MENU

SOURCE CODE [
MenuExample.java]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));
}

}
click on Option button



2 comments: