Javatpoint Logo
Javatpoint Logo

Android Context Menu Example

Android context menu appears when user press long click on the element. It is also known as floating menu.

It affects the selected content while doing action on it.

It doesn't support item shortcuts and icons.

Android Context Menu Example

Let's see the simple example of context menu in android.

activity_main.xml

Drag one listview from the pallete, now the xml file will look like this:

File: activity_main.xml

main_menu.xml

Create a separate menu_main.xml file in menu directory for menu items.

Activity class

Let's write the code to display the context menu on press of the listview.

File: MainActivity.java


Output:

android context menu example output 1

Output after long press on the listview.

android context menu example output 2

Output after clicking on the context menu.

android context menu example output 3






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA