Kotlin Android Popup Menu
Android Popup Menu is a list menu that appears vertically to the view. The popup menu appears below the view if there is room otherwise, appears above. Touching outside to popup menu makes it disappear.
Popup Menu provides actions that are related to the specific content, and it does not affect the corresponding content.
The PopupMenu.OnMenuItemClickListener interface receives the menu items click events if the items don't have their click listener.
Kotlin Android Popup Menu Example
In this example, we will add the popup menu items on a view (button). Clicking on the button shows the popup menu items on which we can perform the relevant action.
Create an android project and select the Basic Activity. This activity auto-generates codes for the menu.
Add the following code in layout directory in the activity_main.xml file. This code is auto-generated while creating the Basic Activity.
Add the following code in the content_main.xml file in layout directory. In this layout, we place a Button View component.
Create a menu resource file named as popup_menu.xml in the menu directory. Add the item tag which creates the menu item for the popup menu.
Add the following code in the MainActivity.kt class. In this class, we implement the PopupMenu.OnMenuItemClickListener interface which receives the menu items click events. To display the popup menu, call the PopupMenu.show() method.