Android Popup Menu

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

activity_main.xml

It contains only one button.

  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/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.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:layout_marginLeft="62dp"  
  18.         android:layout_marginTop="50dp"  
  19.         android:text="Show Popup" />  
  20.   
  21. </RelativeLayout>  

popup_menu.xml

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

  1. <menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <item  
  4.         android:id="@+id/one"  
  5.         android:title="One"/>  
  6.       
  7.     <item  
  8.         android:id="@+id/two"  
  9.         android:title="Two"/>  
  10.         
  11.     <item  
  12.         android:id="@+id/three"  
  13.         android:title="Three"/>  
  14.           
  15. </menu>  

Activity class

It displays the popup menu on button click.

File: MainActivity.java

  1. package com.javatpoint.popupmenu;  

  2. import android.os.Bundle;  

  3. import android.app.Activity;  

  4. import android.view.Menu;  

  5. import android.view.MenuItem;  

  6. import android.view.View;  

  7. import android.view.View.OnClickListener;  

  8. import android.widget.Button;  

  9. import android.widget.PopupMenu;  

  10. import android.widget.Toast;  

  11. public class MainActivity extends Activity {  

  12. Button button1;  

  13.            

  14.          @Override  

  15.          protected void onCreate(Bundle savedInstanceState) {  

  16.           super.onCreate(savedInstanceState);  

  17.           setContentView(R.layout.activity_main);  

  18.             

  19.           button1 = (Button) findViewById(R.id.button1);  

  20.           button1.setOnClickListener(new OnClickListener() {  

  21.            

  22.            @Override  

  23.            public void onClick(View v) {  

  24.             //Creating the instance of PopupMenu  

  25.             PopupMenu popup = new PopupMenu(MainActivity.this, button1);  

  26.             //Inflating the Popup using xml file  

  27.             popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  

  28.            

  29.             //registering popup with OnMenuItemClickListener  

  30.             popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  

  31.              public boolean onMenuItemClick(MenuItem item) {  

  32.               Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  

  33.               return true;  

  34.              }  

  35.             });  

  36.   

  37.             popup.show();//showing popup menu  

  38.            }  

  39.           });//closing the setOnClickListener method  

  40.          }  

  41.     }  

Output:

android popup menu example output 1 android popup menu example output 2 android popup menu example output 3