Charlie Calvert on Elvenware

Writing Code and Prose on Computers



To view an Android menu in VirtualBox or some copies of the Emulator, try the F2 key, or the right popup menu key found on many keyboards between the right-alt key and the right-ctrl key.

Coding a Menu with Android SDK

Create a new project. Target the most recent SDK, but set the min-sdk for the version of Android that you want to use.

You now need to create your menu. Right click on the res folder and create a new folder called menu. Right click on the new folder and choose New | Other | Android | Android XML file or, alternatively, New | Android XML File. Give you file a name, such as menu_main.xml. Enter XML like that shown below:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="" >
  <group android:id="@+id/group_main">
    <item android:id="@+id/item_show" android:title="show"></item>
    <item android:id="@+id/item_exit" android:title="exit"></item>

Modify your main layout so that it has at least one TextView with an id:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
  android:orientation="vertical" >

  android:text="@string/hello" />


Now add simple menu code, in your Activity:

    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflator = getMenuInflater();
    	inflator.inflate(, menu);
    	return super.onCreateOptionsMenu(menu);
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	TextView textView = (TextView)this.findViewById(;
    	return super.onMenuItemSelected(featureId, item);

In Eclipse, the outline of each of the methods shown here can be created automagically if you type in the first few letters of their name, such as onCreateO and then press Ctrl-Space.

The code shown here makes the menu visible, and displays the titles of the menu items selected by the user in a TextView. Notice the calls to getMenuInflater(), inflate() and findViewById. The first two make your menu visiable, the latter helps you locate your TextView so that you can change the text inside it.

The location of menus differs by version of the SDK. The menu moves to the top if you write this:

<uses-sdk android:minSdkVersion="13" />

It then appears on the bar at the top, rather than at the bottom.

You can add a menu using setChildScene. But this is the same way that we set a control.