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

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Related Articles

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • John Clinton Mwangi

    How can I make this work for lower API. My app is for android 2.3 API 9. Please help. Thanks

  • ravishankar yadav

    i want popmenu show with icon and also source code to change the icon of menu item at runtime…becoz i use menu as off on service so on off change icon and On change another icon

  • jyotsana

    vry nice article!
    this may also help u :
    Show Simple Pop-up Menu in Android

  • Bishal

    how can we set a custom layout for the pop up menu. I mean your Button’s width and pop up menu’s width must be same so in that case