Dark Mode
Image

Android Fragments

Android Service

Android AlarmManager

Camera Tutorial

Sensor Tutorial

Android Graphics

Android Animation

Android Web Service

Android MCQ

Android Quiz

Popup Menu

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.

File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.popupmenu.MainActivity">  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/button"  
        android:text="Click"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
  
</android.support.constraint.ConstraintLayout> 

popup_menu.xml

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

File: poupup_menu.xml

<?xml version="1.0" encoding="utf-8"?>  
<menu xmlns:android="http://schemas.android.com/apk/res/android">  
    <item  
        android:id="@+id/one"  
        android:title="One" />  
    <item  
        android:id="@+id/two"  
        android:title="Two"/>  
    <item  
        android:id="@+id/three"  
        android:title="Three"/>  
</menu>  

Activity class

It displays the popup menu on button click.

File: MainActivity.java

package example.javatpoint.com.popupmenu;  
  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.MenuItem;  
import android.view.View;  
import android.widget.Button;  
import android.widget.PopupMenu;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
Button button;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        button = (Button) findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                //Creating the instance of PopupMenu  
                PopupMenu popup = new PopupMenu(MainActivity.this, button);  
                //Inflating the Popup using xml file  
                popup.getMenuInflater().inflate(R.menu.popup_menu, 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;  
                    }  
                });  
  
                popup.show();//showing popup menu  
            }  
        });//closing the setOnClickListener method  
    }  
}  

Output:

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

Comment / Reply From