Android Popup Menu

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. 🙂

Download Source Code

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)