Hello all…

This is a simple tutorial for creating a simple popup menu like the one you see in the action bar options button.
Actually this is fairly simple with actionBar. But if you don’t want the actionbar and only want the popup menu, then here is the solution.

PopUp in Android

You can read more about PopupMenu here

Note : The “PopupMenu” Class is only supported in versions from 3.0 (HoneyComb). So you must Set your project SDK to 3.0 or greater while testing this post.

Android 3.0(API Level 11) provide android.widget.PopupMenu class, to displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

So here we start…

At first what you have to do is

1. Create a folder named “menu” in the “res” folder.

then create an XML named “popup.xml” in it and copy these contents to it.

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



Now in your activity copy this code.
My Activity is named “PopupMenu1″…
So my code look like this…

package com.example.popuptest;

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

public class PopupMenu1 extends Activity {
	protected void onCreate(Bundle savedInstanceState) {

	public void onPopupButtonClick(View button) {
		PopupMenu popup = new PopupMenu(this, button);
		popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

		popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
						"Clicked popup menu item " + item.getTitle(),
				return true;



  1. Pingback: GridView RandomFade animation in Android

  2. Pingback: How to display a context menu from a fragment? | Free Tools Successful Bloggers Are Using

  3. Bhargav September 18, 2013 at 9:03 am

    how to add the image in popup menu

  4. Ans January 1, 2014 at 11:29 am

    When I use your popup.xml it fails. It complains about the last , and if I take it away, the project fails to get built.

    • Ans January 1, 2014 at 11:33 am

      OK, the line android:icon=”@android:drawable/ic_menu_share” fails in my project … why would that be?

      • James January 5, 2014 at 5:02 pm

        Use some other resource that is present in your project.

    • James January 5, 2014 at 5:03 pm

      what error are u getting?

  5. ah June 26, 2015 at 5:26 pm

    im having problem with this code. when i run this, my app just crashes .

    • James June 28, 2015 at 1:53 pm

      Hi, can you check the Logcat and paste the error here.


Leave a Reply to James Cancel

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.