Search This Blog

Saturday 14 July 2012

Sample Code - Creating Option Menu in Android


Sample Code - Create a Option Menu for Android Application

The option menu is a core menu of  the application which provides the basic navigation for applications.

I have written this post the explain the use of option menu in android application.


The best way to create an option menu is to define the option menu in a XML file and inflate the menu resource in onCreateOptionMenu method.

Create a project and define an activity as CreativeMenus (you can use any other name for your activity).


Create the XML file in res->layout directory as menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action1"
          android:icon="@drawable/ic_launcher"
          android:title="@string/action1"
          />
    <item android:id="@+id/action2"
          android:icon="@drawable/ic_launcher"
          android:title="@string/action2" />
    <item android:id="@+id/action3"
          android:icon="@drawable/ic_launcher"
          android:title="@string/action3" />
    <item android:id="@+id/action4"
          android:icon="@drawable/ic_launcher"
          android:title="@string/action4" />
</menu>

Now implement the activity class as follows

package com.example.creativeapps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CreativeMenus extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
         switch (item.getItemId()) {
            case R.id.action1: //start another activity
                startActivity(new Intent(this, CreativelayoutsActivity.class));
                return true;
            case R.id.action2:
                //TODO: handle the code for action 2
                return true;
            case R.id.action3:
                //TODO: handle the code for action 3
                return true;
            case R.id.action4:
                //TODO: handle the code for action 4
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}


To handle the click events on action items you need to implement the onOptionsItemSelected method of your activity class. Get the unique id defined for each item to handle the proper action for click on menu item.


See in the above example we are starting the another activity on click of action1. you can extend the example by implementing your own actions.


Menus are good to implement for version lower then 3.0 for Android version 3.0 or greater implement the action bar.


Please provide your feedback and share this to your creative android friends.


Creative Android Apps




2 comments: