Android Custom Options Menu Button Example
Certainly, there may be times your application is required to make a move beyond showing a typical menu once the user presses the actual MENU button.- We can easily accomplish this particular simply by intercept the particular KeyEvent for your menu button and showing a custom view as an alternative. This can be carried out inside of an Activity or even View simply by overriding the actual onKeyDown() as well as onKeyUp() method.
- Be Aware that Activity.onKeyDown() as well as Activity.onKeyUp() are only called if nothing of its child views control the event initially. It is crucial that you really return a true value whenever consuming these events so that they do not get incorrectly passed up the cycle.
- The following example shows an Activity which demonstrates a customized group of buttons packaged within a basic AlertDialog instead of the regular options menu once the user presses the MENU key.
Creating Layout for Option Menu with Default Icon
res/layout/custom_menu.xml
<?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="horizontal"
android:gravity="bottom">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@android:drawable/ic_menu_add"
android:contentDescription="@string/image_des" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@android:drawable/ic_menu_call"
android:contentDescription="@string/image_des"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="@string/image_des" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@android:drawable/ic_menu_compass"
android:contentDescription="@string/image_des" />
</LinearLayout>
Note we have used “@android:/drawable” It will take the default icon
android:src="@android:drawable/ic_menu_compass"


Creating Layout for Option Menu with Custom Icon
res/layout/custom_menu.xml
<?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="horizontal"
android:gravity="bottom">
<ImageButton
android:id="@+id/sendBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/gmail"
android:contentDescription="@string/image_des" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/android"
android:contentDescription="@string/image_des"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/filer"
android:contentDescription="@string/image_des" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/settings"
android:contentDescription="@string/image_des" />
</LinearLayout>
Note we have used “@drawable” It will take the icon which is at drawable folder
android:src="@drawable/gmail"
Java Code To Configure Options Menu
src/CustomMenuActivity.java
package com.vimaltuts.android.custommenu;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
public class CustomMenuActivityDemo extends ListActivity {
MenuDialog customMenuDialog;
private static final String[] items = { "Android", "Bluetooth", "Chrome",
"Docs", "Email", "Facebook", "Google", "Hungary", "Iphone",
"Korea", "Machintosh", "Nokia", "Orkut", "Picasa", "Singapore",
"Talk", "Windows", "Youtube" };
private ArrayList item = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
initAdapter();
registerForContextMenu(getListView());
}
private class MenuDialog extends AlertDialog {
public MenuDialog(Context context) {
super(context);
setTitle("Android Option Menu Example");
View cus_menu = getLayoutInflater().inflate(R.layout.custom_menu_layout, null);
setView(cus_menu);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
dismiss();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (customMenuDialog == null) {
customMenuDialog = new MenuDialog(this);
}
customMenuDialog.show();
return true;
}
return super.onKeyUp(keyCode, event);
}
private void initAdapter() {
item = new ArrayList();
for (String str : items) {
item.add(str);
}
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, item));
}
}
Output Screen of the Android Custom Option Menu Example

So far we have play with the Android Custom Options Menu through this Android Tutorial for Beginners : Custom Options Menu Button Example Tutorial. On the next tutorial we gonna see about the Android ActionBar Menu Google+
VimalTuts Code Junction 
i got error while i run this code in the elumrator. the error is unfortunatly custummenuactivity has sotopped
Check your Manifest file. In activity tag you need to give your activity java file name in the android:name attribute.
android:name=”.CustomMenuActivityDemo”
now the application run but it show only list but not shows any button there which i add in layout .. why? help me
Did you click the Menu Button in the Right side layout of the Emulator?
Only By clicking that you will see the Menu.
how is your above application work….?
i didn’t understand this….
Hi,
How can I catch click on the buttons?
thanks very very useful for me,
thanks sir