Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Overwrite MENU key to create custom menu


Overwrite MENU key to create custom menu

When user click on the hardware MENU key, Android OS will pop-up option menu; refer to the post "Create OptionsMenu in /res/menu/menu.xml" to know how to implement a formal OptionMenu. Alternatively, we can overwrite the default menu to create our custom menu as shown below:

Custom Menu

Create /res/layout/menu.xml to make the layout of our manu. You can replace the android:src items for your own icon.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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:id="@+id/opt1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
<ImageButton
 android:id="@+id/opt2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
<ImageButton
 android:id="@+id/opt3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:src="@drawable/icon"/>
</LinearLayout>


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.CustomMenu;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
 
public class CustomMenuActivity extends Activity {
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
   }
   
   @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);
   }
   
   MenuDialog menuDialog;
   private class MenuDialog extends AlertDialog{
     
    ImageView Option1, Option2, Option3;
     
    public MenuDialog(Context context) {
     super(context);
     setTitle("Custom Menu");
     View menu = getLayoutInflater().inflate(R.layout.menu, null);
     setView(menu);
      
     Option1 = (ImageView)menu.findViewById(R.id.opt1);
     Option2 = (ImageView)menu.findViewById(R.id.opt2);
     Option3 = (ImageView)menu.findViewById(R.id.opt3);
      
     Option1.setOnClickListener(new View.OnClickListener() {
     
    public void onClick(View v) {
     // TODO Auto-generated method stub
     Toast.makeText(CustomMenuActivity.this, "Option 1", Toast.LENGTH_LONG).show();
     dismiss();
    }
   });
      
     Option2.setOnClickListener(new View.OnClickListener() {
     
    public void onClick(View v) {
     // TODO Auto-generated method stub
     Toast.makeText(CustomMenuActivity.this, "Option 2", Toast.LENGTH_LONG).show();
     dismiss();
    }
   });
      
     Option3.setOnClickListener(new View.OnClickListener() {
       
      public void onClick(View v) {
       // TODO Auto-generated method stub
       Toast.makeText(CustomMenuActivity.this, "Option 3", Toast.LENGTH_LONG).show();
       dismiss();
      }
     });
      
    }
   }
}


akm
www.cdacians.com

No comments:

Post a Comment