Custom Options Menu Button Example

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"
Default Android Icon — Options Menu Without SetTitle() in Java Code

Default Android Icon — Options Menu Without SetTitle() in Java Code

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

Options Menu Without SetTitle()

Options Menu Without SetTitle()

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

7 comments

  1. i got error while i run this code in the elumrator. the error is unfortunatly custummenuactivity has sotopped

  2. how is your above application work….?
    i didn’t understand this….

  3. Hi,

    How can I catch click on the buttons?

  4. thanks very very useful for me,
    thanks sir

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
62 people follow VimalTuts
minkyuuuM_SaifurmanognyaengmomanmohammedvedpawardeeprojeShajeelA