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







44 comments:

  1. it's nice blog... thnx to you..

    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. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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






    ReplyDelete
  16. Such an great and nice blog, Thank you for sharing this wonderful post.


    Data Science

    ReplyDelete
  17. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    date analytics certification training courses
    data science courses training

    ReplyDelete


  18. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.



    DATA SCIENCE COURSE MALAYSIA

    ReplyDelete
  19. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete

  20. I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more.

    Data Science Course

    ReplyDelete
  21. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.



    data science course malaysia

    ReplyDelete
  22. 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.
    blue prism Training in Electronic City

    ReplyDelete
  23. So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site.
    pmp certification malaysia

    ReplyDelete
  24. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.Digital Marketing Training In Vizag

    ReplyDelete
  25. Wow what a Great Information about World Day its incredibly charming instructive post. An obligation of appreciation is all together for the post.ser page.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  26. it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points.



    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery




    ReplyDelete
  27. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    I am looking for and I love to post a comment that "The content of your post is awesome" Great work!Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training


    ReplyDelete
  28. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing data science training in Hyderabad

    ReplyDelete
  29. I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site.
    machine learning courses in bangalore

    ReplyDelete

  30. hello sir,
    thanks for giving that type of information. I am really happy to visit your blog.Leading Solar company in Andhra Pradesh

    ReplyDelete

  31. hello sir,
    thanks for giving that type of information. I am really happy to visit your blog.Leading Solar company in Andhra Pradesh

    ReplyDelete
  32. Such a very useful article. Very interesting to read this article. I would like to thank you for the efforts you had made for writing this awesome article.Cyber Security in Visakhapatnam. Cyber Security near me

    ReplyDelete
  33. I need to thank you for this particularly fantastic article. I definitely really liked every part of it.Educational Institute in Visakhapatnam.

    ReplyDelete
  34. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    Best Gym in Visakhapatnam

    ReplyDelete