![]() |
VOOZH | about |
In Android, there are three types of menus available to define a set of options and actions in the Android apps. Here in this article let's discuss the detail of the Context Menu. In Android, the context menu is like a floating menu and arises when the user has long-pressed or clicked on an item and is beneficial for implementing functions that define the specific content or reference frame effect. The Android context menu is alike to the right-click menu in Windows or Linux. In the Android system, the context menu provides actions that change a specific element or context frame in the user interface and one can provide a context menu for any view. The context menu will not support any object shortcuts and object icons.
A sample GIF is given below to get an idea about what we are going to do in this article.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
The code for that has been given in both Java and Kotlin Programming Language for Android.
Open res > Layout > activity_main.xml and write the following code. In this file add only a TextView to display a simple text.
activity_main.xml:
Open the app > Java > Package > Mainactivity.kt file. In this step, add the code to show the ContextMenu. Whenever the app will start make a long click on a text and display the number of options to select of them for specific purposes. Comments are added inside the code to understand the code in more detail.
MainActivity File:
Now connect the device with a USB cable or in an Emulator and launch the application. The user will see a text. Now long pressing on the text will generate menu options and select one of them to perform specific functionality.