Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "context menu" & method registerForContextMenu()

In Activity class, there is method called registerForContextMenu(View view).

The android document explains that this method is used to registers a context menu to be shown for the given view (multiple views can show the context menu).

  • What does "context menu" mean? Does it mean the physical menu button or what?
  • I also need some explanation about the method registerForContextMenu(View view), it is not clear to me to just read the document online.
like image 332
Leem.fin Avatar asked Feb 02 '12 15:02

Leem.fin


People also ask

What is a context menu?

A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.

What is the function of context menu?

Context menus enable you to provide choices to users for interaction with graphics objects. Program a context menu when you want user to be able to: Choose among specific options by right-clicking a graphics object.

What is context menu Class 5?

A context menu (also called contextual, shortcut, and pop up or pop-up menu) is a menu in a graphical user interface (GUI) that appears upon user interaction, such as a right-click mouse operation.

What is the context menu in apps?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.


2 Answers

It's basically a pop-up menu that is displayed when you long-click certain UI elements (usually an item in a ListView).

You should take a look at the Menus section of the Developer Guide.

like image 124
Chris Avatar answered Oct 22 '22 16:10

Chris


This is from Android Developers: Menu -Android Developers

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

Imagine that you want a conext menu in a listview

//Constants for context menu options
public static final int MENU_MARK = 1;
public static final int MENU_REMOVE = 2;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    ...
    ...
    // Specify that your listview has a context menu attached
    registerForContextMenu(getListView());
}

// here you create the context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, 
   ContextMenuInfo menuInfo) {
  menu.add(Menu.NONE, MENU_MARK, Menu.NONE, "MARK");
  menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, "Remove");
}

// This is executed when the user selects an option
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
    case MENU_MARK:
        mark_item(info.id);
        return true;
    case MENU_REMOVE:
        delete_item(info.id);
        return true;
    default:
        return super.onContextItemSelected(item);
   }
}
like image 24
i.masm Avatar answered Oct 22 '22 16:10

i.masm