Thursday, October 14, 2010

Adding a menu to your Android application

This posting attempts to explain in simple terms what is needed to add a menu to your Android application.

Assuming that you have installed needed Android development tools in Eclipse, the SDK and have a simple application running you need to do the following.

  1. add a menu structure defined in xml (in this case menu/title_only.xml)
  2. add some string resources for the titles to display in the menu (values/strings.xml)
  3. inflate the xml structure to a menu object in your Android application
I am going to create the menu shown in the following screenshot

    The 2 first files can be seen here in the project structure in Eclipse


    title_only.xml is about as simple as they come without icons or submenus and it looks like this
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menuoptions"
            android:title="@string/menuoptions" />
    
        <item android:id="@+id/menuabout"
            android:title="@string/menuabout" />
    </menu>
    

    And the strings.xml contains the labels/titles to be used for the menu

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    ...
     <string name="menuoptions">Options</string>
     <string name="menuabout">About</string>
    ...
    </resources>
    

    Now we need to display this menu when someone clicks the menu button on the phone. The menu button click is tied to the currently shown Activity (form or screen). In my case I want to show this menu on the main screen of my application which is the Droidulus class (Droidulus.java).

    The class representing this Activity has a few methods that can be overridden that are relevant to menus.


    boolean onCreateOptionsMenu(Menu menu)
    boolean onOptionsItemSelected(MenuItem item)
    

    In the following very simple implementations I am inflating the menu to actually generate the menu shown on the Android device

    private Menu menu;
    
    ...
    
    /** Called when clicking the menu button. */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.title_only, menu);
        
        return true;
    }


    Now we need to react to the actual menu item clicks and this happens in the following method


    /** Called when a menu item in the menu is clicked. */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
      case R.id.menuabout:
       Toast.makeText(this, "Droidulus 1.0 by Kenneth Thorman", Toast.LENGTH_SHORT).show();
       return true;
    
      case R.id.menuoptions:
       Intent intent = new Intent(Droidulus.this, OptionsScreen.class);
       startActivityForResult(intent, OPTIONS_ACTIVITY_REQUEST_CODE);
       return true;
       
      // Generic catch all for all the other menu resources
      default:
       if (!item.hasSubMenu()) {
        return true;
       }
       break;
     }
     
     return false;
    }
    


    The menuabout menu item yields the following screenshot



    with regards to the menuoptions menu item click that is the subject of another posting dealing with sub forms/activities and reacting to when those forms are closed it can be found here Child windows / activities in Android

    2 comments:

    Unknown said...

    thnx a lot fnd............this was too helpfull code for biginner

    Kenneth Thorman said...

    Hi Savi

    I am happy that you found it useful.

    Regards
    Kenneth Thorman