Android UI How to - Add menu to activity








The following code shows how to add menu to activity.

Example

layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click on the option menu and play with the choices!"
    />
</LinearLayout>

Menu file:main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/close"
    android:title="Close"
    android:orderInCategory="3"
    android:icon="@drawable/ic_launcher" />
  <item android:id="@+id/no_icon"
    android:orderInCategory="2"
    android:title="Sans Icon" />
  <item android:id="@+id/disabled"
    android:orderInCategory="4"
    android:enabled="false"
    android:title="Disabled" />
  <group android:id="@+id/other_stuff"
    android:menuCategory="secondary"
    android:visible="false">
    <item android:id="@+id/later"
      android:orderInCategory="0"
      android:title="2nd-To-Last" />
    <item android:id="@+id/last"
      android:orderInCategory="1"
      android:title="Last" />
  </group>
  <item android:id="@+id/submenu"
    android:orderInCategory="3"
    android:title="A Submenu">
    <menu>        
      <item android:id="@+id/non_ghost"
        android:title="Non-Ghost"
        android:visible="true"
        android:alphabeticShortcut="n" />
      <item android:id="@+id/ghost"
        android:title="A Ghost"
        android:visible="false"
        android:alphabeticShortcut="g" />
    </menu>
  </item>
</menu>

Java code

package com.java2s.myapplication3.app;
//  w  w  w  .ja  va 2 s .  c o m
import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {
    private final static Map<Integer,String> MESSAGES;
    private boolean otherStuffVisible=false;
    private Menu theMenu=null;

    static {
        MESSAGES=new HashMap<Integer,String>();
        MESSAGES.put(R.id.close, "close");
        MESSAGES.put(R.id.no_icon, "no icon");
        MESSAGES.put(R.id.later, "later");
        MESSAGES.put(R.id.last, "last");
        MESSAGES.put(R.id.non_ghost, "ghost");
        MESSAGES.put(R.id.ghost, "Boo!");
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        theMenu=menu;

        new MenuInflater(getApplication()).inflate(R.menu.main, menu);

        return(super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.non_ghost) {
            otherStuffVisible=!otherStuffVisible;

            theMenu.setGroupVisible(R.id.other_stuff, otherStuffVisible);
        }

        String message=MESSAGES.get(item.getItemId());

        if (message!=null) {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

            return(true);
        }

        return(super.onOptionsItemSelected(item));
    }
}
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
null