Floating Android Context/Popup Menu Example

Floating Android Context Popup Menu Example

In this article i am going to explain about Floating Android Context/Popup Menu Example. Popup Menu or context menu is available from API level 11 (Android 3.0). You may also check some of my previous articles.

Implementation:

To show a context/popup menu on long click,we need to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).

First create a new project and name it as android context/popup menu example. And name the activity as ContextMenuActivity.java. In the layout file add a textview with id tvContextMenu.

Now right click resource folder res and choose New and then select Android Resource Directory and add context_menu.xml like shown below.



Replace the file with below code.

Now in ContextMenuActivity.java file override onCreateContextMenu method to inflate the context_menu.xml After that override onContextItemSelected method to handle the menu item selection. Finally register the context menu to the textview tvContextMenu by calling registerForContextMenu method. The entire code is given below.

Below is the activity_context_menu.xml

Output of android context/popup menu example:

Now if you run the application and long press the tvContextMenu textview you can see the context menu. And while selecting the context/popup menu toast will be displayed. Below is the demo.
Sourcecode of android context/popup menu example:

Output of Floating Android Context Popup Menu Example

 

Source Code:

You can download the Source code of  Floating Android Context/Popup Menu Example by clicking the below download button.

Source code of Floating Android Context/Popup Menu Example

 

 

 

Conclusion:

Hope you learned how to create Floating Android Context/Popup Menu Example.  Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *