Android Popup Menu Example

  • 0

Android Popup Menu Example

Category : Uncategorized

Android Popup Menu Example

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.
Android Popup Menu Example

Let’s see how to create popup menu in android.

It contains only one button.
File: activity_main.xml

  1. <RelativeLayout xmlns:androclass=“”
  2.     xmlns:tools=“”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     tools:context=“.MainActivity” >
  10.     <Button
  11.         android:id=“@+id/button1”
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_alignParentLeft=“true”
  15.         android:layout_alignParentTop=“true”
  16.         android:layout_marginLeft=“62dp”
  17.         android:layout_marginTop=“50dp”
  18.         android:text=“Show Popup” />
  19. </RelativeLayout>





It contains three items as show below. It is created inside the res/menu directory.
File: poupup_menu.xml

  1. <menu xmlns:androclass=“” >
  2.     <item
  3.         android:id=“@+id/one”
  4.         android:title=“One”/>
  5.     <item
  6.         android:id=“@+id/two”
  7.         android:title=“Two”/>
  8.     <item
  9.         android:id=“@+id/three”
  10.         android:title=“Three”/>
  11. </menu>


Activity class

It displays the popup menu on button click.

package com.javatpoint.popupmenu;
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.Toast;
public class MainActivity extends Activity {
Button button1;

protected void onCreate(Bundle savedInstanceState) {

button1 = (Button) findViewById(;
button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(, popup.getMenu());

//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,”You Clicked : ” + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
});;//showing popup menu
});//closing the setOnClickListener method


android popup menu example output 1

android popup menu example output 2

android popup menu example output 3

Leave a Reply