Programming and Personal Development

Android Popup Menus

Popup menus in android are useful for displaying extended options associated with a specific action. For eg: ‘send’ action could have multiple extended options, like ‘send by email’ or ‘send by sms’ etc. Unlike android context menus they can be invoked by any event such a button click not just long clicks. They are associated with the specific view that invoked it and each view in an activity can have its own popup window.

Steps to create a Popup menu

  • Create a new instance of PopupMenu class and pass the instance of the Context ( usually the current activity) and the view (for which the pop-up menu is desired) as arguments.
  • Inflate the menu resource using:
    • popupMenu.inflate () if you are using Android 4.0 SDK and above (or)
    • MenuInflater class (popupMenu.getMenuInflater().inflate() method) if you are using Android 3.0 SDK
  • Call to display the menu

Responding to menu item selections

Override the popupMenu.setOnMenuItemClickListener () method and provide a call back for handling user selections. The use selected menu item is passed in as argument to the callback method.


Android popup menu exampleLets adapt the same example we saw for the android context menu previously to use popup menus instead. What we have here is a single button that is wrapped inside a linear layout. Clicking the button invokes a popup menu that lets us change the background color for the button as shown in the sample screenshot.

Menu resource File

This is the same color menu resource file under res/menu folder that we used in android context menu example.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
    <item android:id="@+id/menu_red" android:title="Red" />
    <item android:id="@+id/menu_green" android:title="Green"/>
    <item android:id="@+id/menu_blue" android:title="Blue"/>

Layout definition file

One single button within a linear layout.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    <Button android:id="@+id/popupMenuBtn"
            android:text="Show me the Popup"

Java Source Code

Nothing fancy here again. We just follow the 3 steps mentioned above for creating a context menu and define the menu handler to handle user selections.

  • The button element acts as the view responsible for invoking the popup menu. Create a new instance of the popup menu and pass the activity and the button as arguments
  • Inflate the menu resource and associate it with the popup menu instance.
  • Call () method within the onClick listener to display the menu when the user clicks the button.
  • Override the popupMenu.setOnMenuItemClickListener () method to change the background color of the button based on user selections.
public class MenuDemo extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
         * A Button that acts as the view element for the popup menu.
        final Button btn = (Button) findViewById(;
         * Step 1: Create a new instance of popup menu
        final PopupMenu popupMenu = new PopupMenu(this, btn);
         * Step 2: Inflate the menu resource. Here the menu resource is
         * defined in the res/menu project folder
         * Step 3: Call show() method on the popup menu to display the
         * menu when the button is clicked.
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
         *  Handle menu item clicks
                new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                return true;



Happy Coding !!!

Web Analytics