---
title: "Show Simple Pop-up Menu in Android"  
description: "Hi everyone, here we are going to learn how to show simple pop-up menus in andriod"  
author: "Anonymous User"  
published: 2014-12-29  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1549/show-simple-pop-up-menu-in-android  
category: "android"  
tags: ["android", "android activity", "android layout", "android edittext"]  
reading_time: 5 minutes  

---

# Show Simple Pop-up Menu in Android

Previously, we see how to show a list view in a [Modal popup](https://www.mindstick.com/articles/13038/crud-operation-searching-paging-in-mvc-with-modal-popup) :Show List View in Model pop-up on [Button click](https://www.mindstick.com/forum/568/check-condion-before-post-page-on-button-click-in-jquery-or-javascript) in Android. The appearnce and feel there is very similar to what we are going to implement is this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370)\

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

Android Popup Menu displays the menu below the [anchor text](https://www.mindstick.com/blog/303273/unlocking-the-potential-of-anchor-text-proven-optimization-techniques) 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.

##### Pre-Requisites:

##### \
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](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) 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](https://yourviews.mindstick.com/view/81617/now-it-is-time-to-say-goodbye-to-internet-explorer) in Eclipse. Open main.xml

Add following code into it:

```
<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity">     <Button          android:id="@+id/showpopup"          android:layout_width="wrap_content"          android:layout_height="wrap_content"           android:text="Show
Popup" /> </RelativeLayout>
```

##### 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">            <item          android:id="@+id/java"          android:title="Java"/>            <item          android:id="@+id/cplus"          android:title="C++"/>              <item          android:id="@+id/python"          android:title="Python"/>       </menu>
```

Showing Pop-up menu on Button Click:

To add [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) to our app we need to show popup menu on button click

So we implement actions on button click by [OnClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach)().

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) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            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                              popup.getMenuInflater().inflate(R.menu.popup_menu,popup_menu.getMenu();                        // registering popup with OnMenuItemClickListener                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                              @Override                              public boolean onMenuItemClick(MenuItem item) {                                    // Show selected item in a Toast                                    Toast.makeText(MainActivity.this,    item.getTitle() + " is Selected",                                                Toast.LENGTH_SHORT).show();
                                    return true;
                              }                        });
                        // showing popup menu                        popup.show();                  }
            });
      }
      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](https://www.mindstick.com/articles/12824/calculator-application-in-android):**\

Our app will look like:

![Show Simple Pop-up Menu in Android](https://www.mindstick.com/mindstickarticle/5b4f5fc5-f08b-4bd6-958c-1a03b3227ccf/images/d3444a56-0020-410b-80ea-298f6b41d363.png) \

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

![Show Simple Pop-up Menu in Android](https://www.mindstick.com/mindstickarticle/5b4f5fc5-f08b-4bd6-958c-1a03b3227ccf/images/838c8d7d-502c-4ae0-894c-3b1cba7ebc3e.png)

Selecting any item [results](https://yourviews.mindstick.com/view/88282/boost-your-career-mindstick-technical-training-that-delivers-results) a Toast message showing your selected item.

![Show Simple Pop-up Menu in Android](https://www.mindstick.com/mindstickarticle/5b4f5fc5-f08b-4bd6-958c-1a03b3227ccf/images/1f65b8b5-f082-4d65-8b10-c9b67aed7d80.png)

\

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

---

Original Source: https://www.mindstick.com/articles/1549/show-simple-pop-up-menu-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
