Mega Code Archive

 
Categories / Android / UI
 

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>