Menu In Android

Creating Menus in Android
This could be done in 2 ways: XML and Java. But the preferred way to do is through XML .Simply, to keep the designing and the logic of the system differently.

Create -> new XML (mymenu.xml) -> Select the Menu (radio button). Through that you make that xml file a menu .On finishing it .We could also see a menu folder has been created in the res folder .

Now to create an item for the menu that is mymenu.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/item1" android:icon="@android:drawable/ic_menu_compass">
<item android:id="@+id/item2"  android:icon="@android:drawable/ic_menu_call">


I am thinking, what the hell is ic_menu_compass right?

Well first of all since we want to put an icon with the menu out there we have customized our item tagby adding an attribute to it “android:icon”. And ic_menu_compass is the System icon that is present in the android.jar package.


Keep going unless you get it.

Then, you see a lot of .png files and in that you will see a group of “ic” prefixed image files. There you could find ic_menu_compass. And, we need a reference to that beautiful thing.
Come to your Java file:

package com.menuinflator;

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

public class MenuInflatorTestActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

	public boolean onPrepareOptionsMenu(Menu menu) { //Called everytime menu is being clicked.
		// TODO Auto-generated method stub
		Toast.makeText(getBaseContext(),"Prepared menu so called",4000).show();
		return super.onPrepareOptionsMenu(menu);

	public boolean onCreateOptionsMenu(Menu menu) { //used for the initialization purposes. Called once
		// TODO Auto-generated method stub
		Toast.makeText(getBaseContext(),"Menu Creating!",4000).show(); 
		return true;

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
			Toast.makeText(getBaseContext(),"Slected" + featureId, 4000).show();
			Toast.makeText(getBaseContext(),"Item2 Selected", 4000).show();
		return true;

Click on the #menu on the Emulator. The toast gets printed. And now even if you click on menu again and again it never gets printed.

onCreateOptionsMenu() : This method has been overridden. And, it gets called just for the first time when the options of the menus are going to be created. Then, never in life will they show their faces. Remember it!
And run the program.

public boolean onOptionsItemSelected (MenuItem item)

Added in API level 1
This hook is called whenever an item in your options menu is selected. The default implementation simply returns false to have the normal processing happen (calling the item’s Runnable or sending a message to its Handler as appropriate). You can use this method for any items for which you would like to do processing without those other facilities.

Derived classes should call through to the base class for it to perform the default menu handling.

item The menu item that was selected.
boolean Return false to allow normal menu processing to proceed, true to consume it here.

public boolean onMenuItemSelected (int featureId, MenuItem item)

Added in API level 1
Default implementation of onMenuItemSelected(int, MenuItem) for activities. This calls through to the new onOptionsItemSelected(MenuItem) method for the FEATURE_OPTIONS_PANEL panel, so that subclasses of Activity don’t need to deal with feature codes.

featureId The panel that the menu is in.
item The menu item that was selected.
Returns boolean

Return true to finish processing of selection, or false to perform the normal menu handling (calling its Runnable or sending a Message to its target Handler).


Android Introduction



Android is a complete operating environment based upon the Linux® V2.6 kernel. Initially, the deployment target for Android was the mobile-phone arena, including smart phones and lower-cost flip-phone devices. However, Android’s full range of computing services and rich functional support have the potential to extend beyond the mobile-phone market. Android can be useful for other platforms and applications. In this article, get an introduction to the Android platform and learn how to code a basic Android application.Introduction

The BlackBerry and iPhone, which have appealing and high-volume mobile platforms, are addressing opposite ends of a spectrum. The BlackBerry is rock-solid for the enterprise business user. For a consumer device, it’s hard to compete with the iPhone for ease of use and the “cool factor.” Android, a young and yet-unproven platform, has the potential to play at both ends of the mobile-phone spectrum and perhaps even bridge the gulf between work and play.

Today, many network-based or network-capable appliances run a flavor of the Linux kernel. It’s a solid platform: cost-effective to deploy and support and readily accepted as a good design approach for deployment. The UI for such devices is often HTML-based and viewable with a PC or Mac browser. But not every appliance needs to be controlled by a general computing device. Consider a conventional appliance, such as a stove, microwave or bread maker. What if your household appliances were controlled by Android and boasted a color touch screen? With an Android UI on the stove-top, the author might even be able to cook something.

In this article, learn about the Android platform and how it can be used for mobile and nonmobile applications. Install the Android SDK and build a simple application. Download the source code for the example application in this article.

A brief history of Android

The Android platform is the product of the Open Handset Alliance, a group of organizations collaborating to build a better mobile phone. The group, led by Google, includes mobile operators, device handset manufacturers, component manufacturers, software solution and platform providers, and marketing companies. From a software development standpoint, Android sits smack in the middle of the open source world.

The first Android-capable handset on the market was the G1 device manufactured by HTC and provisioned on T-Mobile. The device became available after almost a year of speculation, where the only software development tools available were some incrementally improving SDK releases. As the G1 release date neared, the Android team released SDK V1.0 and applications began surfacing for the new platform.

To spur innovation, Google sponsored two rounds of “Android Developer Challenges,” where millions of dollars were given to top contest submissions. A few months after the G1, the Android Market was released, allowing users to browse and download applications directly to their phones. Over about 18 months, a new mobile platform entered the public arena.

Back to top

The Android platform

With Android’s breadth of capabilities, it would be easy to confuse it with a desktop operating system. Android is a layered environment built upon a foundation of the Linux kernel, and it includes rich functions. The UI subsystem includes:

  • Windows
  • Views
  • Widgets for displaying common elements such as edit boxes, lists, and drop-down lists

Android includes an embeddable browser built upon WebKit, the same open source browser engine powering the iPhone’s Mobile Safari browser.

Android boasts a healthy array of connectivity options, including WiFi, Bluetooth, and wireless data over a cellular connection (for example, GPRS, EDGE, and 3G). A popular technique in Android applications is to link to Google Maps to display an address directly within an application. Support for location-based services (such as GPS) and accelerometers is also available in the Android software stack, though not all Android devices are equipped with the required hardware. There is also camera support.

Historically, two areas where mobile applications have struggled to keep pace with their desktop counterparts are graphics/media, and data storage methods. Android addresses the graphics challenge with built-in support for 2-D and 3-D graphics, including the OpenGL library. The data-storage burden is eased because the Android platform includes the popular open source SQLite database. Figure 1 shows a simplified view of the Android software layers.