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”
2. Design application launcher
3. Create a blank activity
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
10. Download

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

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
u got a mistake there:
6. Update the file res/layout/activity_main.xml
it should be:
Hi,
Thank you for noting down the mistake.
It is corrected now.
What should the update be? It’s not shown on this thread.Thanks.
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..
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.
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.
It is having min sdk version 11, while for gingerbread (i.e. sdk version 8, the same is not available i.e. the PopupMenu
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
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.
Even I am trying to change background color
Thanks, it helped me a lot!
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.
If ew have to change menu layout colour???
Hello, can you explain me how can I implement a pop up by clicking a menu item??
how to set icon in popup manu item.give me the code