001.
package
com.myapp;
002.
003.
import
android.os.Bundle;
004.
import
android.view.ContextMenu;
005.
import
android.view.ContextMenu.ContextMenuInfo;
006.
import
android.view.Menu;
007.
import
android.view.MenuItem;
008.
import
android.view.View;
009.
import
android.widget.Button;
010.
import
android.widget.Toast;
011.
import
android.app.Activity;
012.
013.
014.
public
class
MainActivity
extends
Activity {
015.
016.
String[] Cmd1 = {
"Command1"
,
"Command2"
,
"Command3"
,
"Command4"
};
017.
String[] Cmd2 = {
"Command5"
,
"Command6"
,
"Command7"
,
"Command8"
};
018.
019.
Button btn1;
020.
Button btn2;
021.
022.
String btnClick;
023.
024.
@Override
025.
public
void
onCreate(Bundle savedInstanceState) {
026.
super
.onCreate(savedInstanceState);
027.
setContentView(R.layout.activity_main);
028.
029.
030.
btn1 = (Button)findViewById(R.id.button1);
031.
032.
033.
btn2 = (Button)findViewById(R.id.button2);
034.
035.
registerForContextMenu(btn1);
036.
registerForContextMenu(btn2);
037.
}
038.
039.
@Override
040.
public
void
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
041.
042.
menu.setHeaderIcon(android.R.drawable.btn_star_big_on);
043.
044.
if
(v == btn1) {
045.
btnClick =
"Cmd1"
;
046.
String[] menuItems = Cmd1;
047.
menu.setHeaderTitle(
"Menu 1"
);
048.
for
(
int
i =
0
; i<menuItems.length; i++) {
049.
menu.add(Menu.NONE, i, i, menuItems[i]);
050.
}
051.
}
else
if
(v == btn2) {
052.
btnClick =
"Cmd2"
;
053.
String[] menuItems = Cmd2;
054.
menu.setHeaderTitle(
"Menu 2"
);
055.
for
(
int
i =
0
; i<menuItems.length; i++) {
056.
menu.add(Menu.NONE, i, i, menuItems[i]);
057.
}
058.
}
059.
060.
061.
}
062.
063.
@Override
064.
public
boolean
onContextItemSelected(MenuItem item) {
065.
int
menuItemIndex = item.getItemId();
066.
067.
068.
if
(btnClick ==
"Cmd1"
)
069.
{
070.
071.
String[] menuItems = Cmd1;
072.
String CmdName = menuItems[menuItemIndex];
073.
074.
075.
if
(
"Command1"
.equals(CmdName)) {
076.
Toast.makeText(MainActivity.
this
,
"Your Selected Command1"
,Toast.LENGTH_LONG).show();
077.
078.
079.
080.
081.
}
else
if
(
"Command2"
.equals(CmdName)) {
082.
Toast.makeText(MainActivity.
this
,
"Your Selected Command2"
,Toast.LENGTH_LONG).show();
083.
084.
085.
086.
087.
}
else
if
(
"Command3"
.equals(CmdName)) {
088.
Toast.makeText(MainActivity.
this
,
"Your Selected Command3"
,Toast.LENGTH_LONG).show();
089.
090.
091.
092.
093.
}
else
if
(
"Command4"
.equals(CmdName)) {
094.
Toast.makeText(MainActivity.
this
,
"Your Selected Command4"
,Toast.LENGTH_LONG).show();
095.
096.
097.
098.
099.
}
100.
101.
}
102.
103.
104.
if
(btnClick ==
"Cmd2"
)
105.
{
106.
107.
String[] menuItems = Cmd2;
108.
String CmdName = menuItems[menuItemIndex];
109.
110.
111.
if
(
"Command5"
.equals(CmdName)) {
112.
Toast.makeText(MainActivity.
this
,
"Your Selected Command5"
,Toast.LENGTH_LONG).show();
113.
114.
115.
116.
117.
}
else
if
(
"Command6"
.equals(CmdName)) {
118.
Toast.makeText(MainActivity.
this
,
"Your Selected Command6"
,Toast.LENGTH_LONG).show();
119.
120.
121.
122.
123.
}
else
if
(
"Command7"
.equals(CmdName)) {
124.
Toast.makeText(MainActivity.
this
,
"Your Selected Command7"
,Toast.LENGTH_LONG).show();
125.
126.
127.
128.
129.
}
else
if
(
"Command8"
.equals(CmdName)) {
130.
Toast.makeText(MainActivity.
this
,
"Your Selected Command8"
,Toast.LENGTH_LONG).show();
131.
132.
133.
134.
135.
}
136.
137.
}
138.
139.
return
true
;
140.
}
141.
142.
143.
@Override
144.
public
boolean
onCreateOptionsMenu(Menu menu) {
145.
getMenuInflater().inflate(R.menu.activity_main, menu);
146.
return
true
;
147.
}
148.
149.
}