Home > DeveloperSection > Articles > Show Simple Pop-up Menu in Android

Show Simple Pop-up Menu in Android

Android Android  Android Activity  Android Layout  Android Edittext 
0 Comment(s)
 3831  View(s)
Rate this:

Show Simple Pop-up Menu in Android


Previously, we see how to show a list view in a Modal popup :Show List View in Model pop-up on Button click in Android. The appearnce and feel there is very similar to what we are going to implement is this article

In this article we are going to implement pop-up menus on button click.

Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

The android.widget.PopupMenu is the direct subclass of java.lang.Object class.


1.       Ellipse SDK

2.       Android SDK

3.       ADT plugin

Or Android Studio and a compatible version of JAVA SDK

Install and configure the above utilities.

Now create a new Android project namely “PopupMenu”.

Add Widgets to our GUI:

Now, we need to add views in our view groups, i.e. add widgets to our GUI. We just need a button widget for our app’s GUI

For this, navigate to res/Layout/main.xml from package explorer in Eclipse. Open main.xml

Add following code into it:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"








    tools:context=".MainActivity" >






        android:text="Show Popup" />





Add a Pop- up Menu Layout:

Now, we need to create our menu Layout. It contains three items as show below. It is created inside the res/menu directory. Name it popup_menu.xml

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

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

















Showing Pop-up menu on Button Click:

To add behavior to our app we need to show popup menu on button click

So we implement actions on button click by OnClickListener().

So our final code for  mainActivity class will look like this:


package com.example.popupmenu;


import android.os.Bundle;

import android.app.Activity;

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.Toast;


public class MainActivity extends Activity {

      Button showpop;


      protected void onCreate(Bundle savedInstanceState) {




            showpop = (Button) findViewById(R.id.showpopup);

            showpop.setOnClickListener(new OnClickListener() {


                  public void onClick(View v) {

                        // Create instance of PopupMenu

                        PopupMenu popup = new PopupMenu(MainActivity.this, showpop);


                        // Inflating the Popup using Xml file



                        // registering popup with OnMenuItemClickListener

                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {



                              public boolean onMenuItemClick(MenuItem item) {

                                    // Show selected item in a Toast


                                                item.getTitle() + " is Selected",


                                    return true;




                        // showing popup menu







      public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.

            getMenuInflater().inflate(R.menu.main, menu);

            return true;





Running our application:

Our app will look like:


Now click on “Show Popup” Button, a popup menu is appeared consists of menu items


Selecting any item results a Toast message showing your selected item.

Next we will see how to implement search functionality in list view : Simple Search implementation in android

Thanks for reading this post.

Happy Coding!! J

Don't want to miss updates? Please click the below button!

Follow MindStick