context menu


Context Menu

Context Menu in Android is a floating menu, which might not be there initially but might appear only after a certain action is done. For example, it might appear after a long press of the home button. This kind of phenomenon is also known as the reference frame act. We can compare the context menu with something like right-click menu on Windows. The context menu scopes the actions which are capable of changing a particular context frame in the UI, and a developer can specify a context menu for any kind of view. The following code shows how a context menu can be created in an activity file.

The code will develop something like this as shown below. If you long press on the text, the menu will appear.

Contextual action mode:

It is used for displaying the action items affecting some selected contents in a bar at the screen's top. It also allows the users to select multiple items.

ContextMenu.ContextMenuInfo:

It is the additional information about the context menu creation.

Public methods of the context menu:

  • setHeaderIcon (int iconRes): It set the header icon of the context menu to the one given at iconRes resource id.
  • serHeaderIcon (Drawable icon): It is used to set the header icon of the context menu to the icon in icon Drawable.
  • setHeaderTitle (int titleRes): It is used to set the header title of the context menu to that given in the titleRes resource identifier.
  • setHeaderTitle (CharSequence title): It set the header title of the context menu to that present in the title tag.
  • setHeaderView (View view): It sets the context menu’s header to the view present in the View tag.
Image Not Found