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







21 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
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete