Following steps required to create Context menu in android application. When user click long on any item present on the screen, we usually show option in terms of context menu.
Context menus are bind with the view, you need to get the view and register for context menu as shown following.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.textView);
registerForContextMenu(t);
}
Context menus are bind with the view, you need to get the view and register for context menu as shown following.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.textView);
registerForContextMenu(t);
}
Prepare Menu Items
Override the "onCreateContextMenu" method of Activity class.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Hello");
// Assuming that you already have an array in your string.xml
String[] menuItems = getResources().getStringArray(R.array.menu_options);
String[] menuItems = getResources().getStringArray(R.array.menu_options);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
Define Action on selecting the menu item.
Override the "onContextItemSelected" method of Activity Class.