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.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                return true;

            return false;

    pw.showAsDropDown(view, 0, 0);


Here's the XML layout of the main activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="@string/showPopup" />




  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

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

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

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

  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

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

  7. The best and simplest popup example

  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

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

  10. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Digital Marketing Training in Chennai

    Digital Marketing Training in Bangalore

    digital marketing training in tambaram

    digital marketing training in annanagar

  11. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    full stack developer training in annanagar

    full stack developer training in tambaram

    full stack developer training in velachery

  12. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
    python training institute in chennai
    python training in Bangalore
    python training institute in chennai

  13. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    Blueprism training in Pune

    Blueprism online training

    Blue Prism Training in Pune

    Blueprism training in Chennai

    Blueprism training in Bangalore

  14. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command

    Data Science training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

  15. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

  16. I am happy to find this post very useful for me, as it contains a lot of information. I always prefer to read the quality content I found in you post. Thanks for sharing.

    Selenium training in Chennai
    Selenium Courses in Chennai
    best ios training in chennai
    Digital Marketing Training in Chennai
    .Net coaching centre in chennai
    .Net course in chennai
    best dotnet training in chennai

  17. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Bangalore | Amazon Web Services Training in bangalore , india

    AWS Training in pune | Amazon Web Services Training in Pune, india

    AWS Training in Chennai|Amazon Web Services Training in Chennai,India

    aws online training and certification | amazon web services online training ,india

  18. Hello there! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles.
    nebosh course in chennai

  19. Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

  20. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    Data warehouse training chennai | Data warehousing training chennai