StackTips

Android Popup Menu Example

nilan avtar

Written by

Nilanchala,  3 min read,  2.91K views, updated on Sept. 17, 2023

In example explains how to create Popup menu in android. Popup menu is used to display the global actions. Popup menu is an overflow menu like Spinner actions. PopupMenu is available from API level 11 (Android 3.0).

activity_main.xml

<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:background="#fcfcfc" >

    <Button
        android:id="@+id/btn_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#176CEC"
        android:text="SHOW POPUP"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

</RelativeLayout>

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item_movies"
        android:showAsAction="ifRoom|withText"
        android:title="Movies"
        android:visible="true"/>
    <item
        android:id="@+id/item_music"
        android:showAsAction="ifRoom|withText"
        android:title="Music"
        android:visible="true"/>
    <item
        android:id="@+id/item_comedy"
        android:showAsAction="ifRoom|withText"
        android:title="Comedy"
        android:visible="true"/>

</menu>

PopMenuActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

public class PopMenuActivity extends Activity implements OnMenuItemClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn_click).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				PopupMenu popupMenu = new PopupMenu(PopMenuActivity.this, view);
				popupMenu.setOnMenuItemClickListener(PopMenuActivity.this);
				popupMenu.inflate(R.menu.popup_menu);
				popupMenu.show();
			}
		});
	}

	public boolean onMenuItemClick(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item_comedy:
			Toast.makeText(this, "Comedy Clicked", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_movies:
			Toast.makeText(this, "Movies Clicked", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_music:
			Toast.makeText(this, "Music Clicked", Toast.LENGTH_SHORT).show();
			return true;		
		}
	}
}

Output
Popup Example in Android