A popup menu example in Android

July 25, 2012
By

Popup menu is a modal menu that is anchored on a view. It is either displayed below if there is enough room or above the view. Popup menu is introduced in Android HoneyComb ( API level 11 ) version. In this application, a popup menu will be displayed on clicking a button available in the Main Activity.

This application is developed in Eclipse (3.7.2) with ADT plugin ( 20.0.2 ) and Android SDK ( R20.0.1 ).


1. Create an Android Application project namely “PopupMenuDemo”

Create new Android Application Project

Figure 1 : Create new Android Application Project


2. Design application launcher

Design Application Launcherf

Figure 2 : Design Application Launcherf


3. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity


4. Enter activity details

Enter Activity Details

Figure 4 : Enter Activity Details


5. Update the file res/values/strings.xml

<resources>

    <string name="app_name">PopupMenuDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="str_btn">Open Popup Menu</string>
    <string name="action1">Action1</string>
    <string name="action2">Action2</string>
    <string name="action3">Action3</string>

</resources>


6. Update the file res/layout/activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/str_btn" />
</RelativeLayout>


7. Create a menu file res/menu/popup.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action1"
        android:title="@string/action1"
    />

    <item
        android:id="@+id/action2"
        android:title="@string/action2"
    />

    <item
        android:id="@+id/action3"
        android:title="@string/action3"
    />

</menu>

8. Update the file src/in/wptrafficanalyzer/popupmenudemo/MainActivity.java


package in.wptrafficanalyzer.popupmenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);

        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                /** Instantiating PopupMenu class */
                PopupMenu popup = new PopupMenu(getBaseContext(), v);

                /** Adding menu items to the popumenu */
                popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

                /** Defining menu item click listener for the popup menu */
                popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(getBaseContext(), "You selected the action : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

                /** Showing the popup menu */
                popup.show();

            }
        };

        btn.setOnClickListener(listener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


9. Application in execution

Popup menu is displayed on button click

Figure 5 : Popup menu is displayed on button click


10. Download


11. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

15 Responses to A popup menu example in Android

  1. Ofer s on October 23, 2012 at 7:48 pm

    u got a mistake there:
    6. Update the file res/layout/activity_main.xml
    it should be:

    • george on October 23, 2012 at 8:12 pm

      Hi,
      Thank you for noting down the mistake.
      It is corrected now.

    • Phil Gibbs on October 30, 2012 at 10:59 pm

      What should the update be? It’s not shown on this thread.Thanks.

  2. Rajendra Verma on December 10, 2012 at 3:09 pm

    Hi,
    Thank you for help. I am new in android development. I am developing one industrial project so that I want a person to whom I can share my problem and thought a bout Android development process. kindly help me by replying me..

    thank you once again..

  3. varsha on December 13, 2012 at 12:00 pm

    Hello, nice tutorial.It helps me but i want to know that how to change the background color of the item?When i click the button popup menu is open but the font color and background color are in black.I am not able to see properly what is written in particular item name. Please guide me i am in android.
    Thank you.

    • Pankaj Kushwaha on May 28, 2013 at 3:29 pm

      Did you get anything for changing font and background color ?
      Even I am looking for the same but no luck. If you have something then please share.

  4. Sandeepan Kundu on December 20, 2012 at 10:54 am

    It is having min sdk version 11, while for gingerbread (i.e. sdk version 8, the same is not available i.e. the PopupMenu

  5. atul on December 29, 2012 at 6:06 pm

    Hi,

    I was looking out for similar functionality for popup menu, but it should be nested ? is it possible or should be a custom nested popup created ?

    please assist

  6. daydr3am3r on March 14, 2014 at 1:41 pm

    Hey, I know it’s a little bit too late but how do you change the background and text color?
    I’ve tried different things without any luck.

  7. Prathyusha on November 14, 2014 at 5:15 pm

    Even I am trying to change background color

  8. Artyom on November 22, 2014 at 3:43 am

    Thanks, it helped me a lot!

  9. sandeep on February 19, 2015 at 10:10 am

    Hey George Mathew what if I want to set the background color of this popup menu.what I have to do for setting background for this popup menu.

  10. Gourav Sharma on April 21, 2015 at 3:52 pm

    If ew have to change menu layout colour???

  11. Lisa Pisa on June 21, 2015 at 3:59 am

    Hello, can you explain me how can I implement a pop up by clicking a menu item??

  12. swapnil Yadav on August 18, 2015 at 12:14 pm

    how to set icon in popup manu item.give me the code

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner