Toggle Menu

Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Core Code

OS and Tools

Art

Elvenware

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 resfolder 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="http://schemas.android.com/apk/res/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>
  </group>
</menu>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

<TextView
  android:id="@+id/textview_main"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />

</LinearLayout>

Now add simple menu code, in your Activity:

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflator = getMenuInflater();
        inflator.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        TextView textView = (TextView)this.findViewById(R.id.textview_main);
        textView.setText(item.getTitle());
        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.

Copyright © Charlie Calvert | Elvenware Home | Writing Code | Delphi | CSharp | My Books