Thursday, 17 May 2012

Popup menu - android Sample Code

How to implement a popup menu - Android 2.1 


Popup Menu is available in Android SDK 11 on wards. Here I am implementing the concept of popup menu in SDK 7. A Button when clicked should show a Popup Menu. 

In the onCreate of the main activity we have to inflate the layout of the menu which we intend to show as popup.

 inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 popupView = inflater.inflate(R.layout.menu_layout, null, false);
Here, menu_layout is the desired layout you wish to show as popup.
the onClick method for the button to show the popup is

public void showPopup(View view) {
    pw = new PopupWindow(getApplicationContext());
    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setOutsideTouchable(true);
    pw.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                pw.dismiss();

                return true;
            }

            return false;
        }
    });

    pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(false);
    pw.setContentView(popupView);
    pw.showAsDropDown(view, 0, 0);

}

Here's the XML layout of the main activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dip"
        android:onClick="showPopup"
        android:text="@string/showPopup" />

</LinearLayout>

DOWNLOAD SAMPLE CODE







10 comments:

  1. I’m seriously happy to discover this great site the future of this blog is getting good and more useful for me thanks and god bless you.
    IT Learning

    ReplyDelete
  2. Is calling pw.setOutsideTouchable() twice required?

    ReplyDelete
  3. The best and simplest popup example I've found, great job.

    ReplyDelete
  4. Very nice indeed. But how can be programaticaly a menu item like this disabled/enabled at run time?

    ReplyDelete
  5. i do want create pop up menu from right to left moving in that activity when i click the button want to show the gallery images

    ReplyDelete
  6. Very nice code for popup menu.
    Thank you very much. From Devendra Yadav

    ReplyDelete
  7. The best and simplest popup example

    ReplyDelete
  8. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai

    ReplyDelete
  9. Very informative ..i suggest this blog to my friends..Thank you for sharing
    Android Training in chennai

    ReplyDelete