Android Projects

Android Development Books


Creating a Popup Menu

Figure 4. A popup menu in the Gmail app, anchored to the overflow button at the top-right.

Creating a Popup Menu

A PopupMenu is a modal menu anchored to a View. It appears below the anchor view if there is room, or above the view otherwise. It's useful for:
  • Providing an overflow-style menu for actions that relate to specific content (such as Gmail's email headers, shown in figure 4).
    Note: This is not the same as a context menu, which is generally for actions that affect selected content. For actions that affect selected content, use the contextual action mode or floating context menu.
  • Providing a second part of a command sentence (such as a button marked "Add" that produces a popup menu with different "Add" options).
  • Providing a drop-down similar to Spinner that does not retain a persistent selection.
Note: PopupMenu is available with API level 11 and higher.
If you define your menu in XML, here's how you can show the popup menu:
  1. Instantate a PopupMenu with its constructor, which takes the current application Context and the View to which the menu should be anchored.
  2. Use MenuInflater to inflate your menu resource into the Menu object returned by PopupMenu.getMenu(). On API level 14 and above, you can use PopupMenu.inflate() instead.
  3. Call
For example, here's a button with the android:onClick attribute that shows a popup menu:
    android:onClick="showPopup" />
The activity can then show the popup menu like this:
public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(, popup.getMenu());;
In API level 14 and higher, you can combine the two lines that inflate the menu with PopupMenu.inflate().
The menu is dismissed when the user selects an item or touches outside the menu area. You can listen for the dismiss event using PopupMenu.OnDismissListener.

Handling click events

To perform an action when the user selects a menu item, you must implement the PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling setOnMenuItemclickListener(). When the user selects an item, the system calls the onMenuItemClick() callback in your interface.
For example:

public void showMenu(View v) {
    PopupMenu popup = new PopupMenu(this, v);

    // This activity implements OnMenuItemClickListener
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
            return true;
            return true;
            return false;
More Details: 
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...


Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0