Custom menu : Menu « UI « Android






Custom menu

  

package com.examples.keys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

public class MyActivity extends Activity {

    MenuDialog menuDialog;
    private class MenuDialog extends AlertDialog {

        public MenuDialog(Context context) {
            super(context);
            View menu = getLayoutInflater().inflate(R.layout.custommenu, null);
            setView(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(menuDialog == null) {
                menuDialog = new MenuDialog(this);
            }
            menuDialog.show();
            return true;
        }

        return super.onKeyUp(keyCode, event);
    }

}
//custommenu.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="wrap_content"
  android:orientation="horizontal">
  <ImageButton android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:src="@android:drawable/ic_menu_send" />
  <ImageButton android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:src="@android:drawable/ic_menu_save" />
  <ImageButton android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:src="@android:drawable/ic_menu_search" />
  <ImageButton android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:src="@android:drawable/ic_menu_preferences" />
</LinearLayout>

   
    
  








Related examples in the same category

1.Create Option menu
2.Menu and messages
3.Context menu
4.Using Icon in Menu
5.Option Menu selection event
6.Menu Inflation
7.Context Menu event
8.Create Menu within your code
9.Activity Menu
10.Define menu in xml file
11.Adding submenu
12.Using Icon in menu and submenu
13.Get Menu title
14.onOptionsItemSelected/onPrepareOptionsMenu/onCreateOptionsMenu Event
15.Basics of the Action Bar and how it interoperates with the standard options menu.
16.Demonstration of displaying a context menu from a fragment.
17.Demonstrates how fragments can participate in the options menu.
18.Demonstrates inflating menus from XML.
19.Usage of SearchView in an ActionBar as a menu item.
20.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL