Android Popup Menu - Androhub

Popup Menu Banner

Android Popup Menu

In previous article we learned how to create custom overflow menu. In this article we will be going to learn about Popup Menu which is similar as Overflow Menu.

A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

VIDEO DEMO

Example

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open strings.xml located under res=>values folder and add following strings.

3. Create activity_main.xml and add the following code. Here there are three buttons which will open different types of popup menus.

4. Now let’s create menu for Popup naming popup_menu.xml.

5. Now open your MainActivity.java and add the below code to it. As we will display the popup menu at the anchor of buttons only so we don’t need any other activity or xml.

Here in above MainActivity.java there are three types of Popup Menus as follows:

  • Default Popup Menu: This popup menu don’t need any extra code just inflating a menu to Popup class will do its job.

Popup Menu Banner
Default Popup Menu

  • Custom Popup Menu with Icons : To show the icons next to menu items same like overflow menus we have to add extra code to it.

Custom Popup Menu with Icons
Custom Popup Menu with Icons

  • Styled Popup Menu : To style the popup menu like changing background, text color, etc we have to make custom style and set the style to popup menu like below:

Here is the style code for Popup Menu:

Styled Popup Menu
Styled Popup Menu

6. Full styles.xml code:

7. Finally, all done run your code.

Thanks. 🙂

 

5 Comments

mza
Tuesday, August 27th, 2019

thanks,

Jakub Bober
Wednesday, May 26th, 2021

How to hide navigation bar when PopupMenu is showing?

Attaullah
Saturday, August 21st, 2021

Great

Jongtak
Wednesday, October 26th, 2022

How to change text color of popup menu when menu-item was selected/hovered?
I’ve searched a lot here and there, but I can’t find a way.
Thanks.

Post comment

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