Using the PopupWindow class in Android apps

I need to display some data in a popup window in an Android app. After a brief search on the web I decided to go with the PopupWindow class, but I couldn’t find any good example of how to use it.

There seems to be a lot of questions out on the web about how to work with the PopupWindow, so I thought I should post an example showing how I solved it under Android 2.2 (Froyo).

Create a layout for your PopupWindow

Create an XML layout file that defines the PopupWindow UI just as you do for other UI’s like activities. The only thing to think about here is that the first container element that you define (LinearLayout, AbsoluteLayout etc) needs to have an id defined, as we need to reach it from code later on.

For reference this is how the layout for my PopupWindow is defined, it’s created in a file named popup_layout.xml. You can see the id on the LinearLayout element on row 2 (popup_element).

After this is done, it’s time to write some code.

Initiating and creating the PopupWindow

At first we need to initialize the layout, and for that we need to inflate it, then we use it to create our PopupWindow, after that all we need to do is show it.

    private PopupWindow pw;
    private void initiatePopupWindow() {
        try {
            //We need to get the instance of the LayoutInflater, use the context of this activity
            LayoutInflater inflater = (LayoutInflater) ConfirmActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //Inflate the view from a predefined XML layout
            View layout = inflater.inflate(R.layout.popup_layout,
                    (ViewGroup) findViewById(R.id.popup_element));
            // create a 300px width and 470px height PopupWindow
            pw = new PopupWindow(layout, 300, 470, true);
            // display the popup in the center
            pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

            mResultText = (TextView) layout.findViewById(R.id.server_status_text);
            Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
            makeBlack(cancelButton);
            cancelButton.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pw.dismiss();
        }
    };

On row 8-9 you can see the inflater being called to create the layout. Take care when defining the parameters here, the first one is the reference to the layout xml file, the second parameter is the reference to the id of the element in that file that you want to use as root node for the created PopupWindow.

Then just call the initiatePopupWindow from whatever piece of code you need to trigger the PopupWindow from.

Referencing elements in the PopupWindow

If you want to reference elements in the PopupWindow that you have created, you need to use the layout that was used when creating that window. You can see how I store references to the result text for later use, and define the cancel button and set it’s OnClickListener on rows 15, 16 and 18.

Good luck and happy coding!


More ways to show information to users in Android

You can also use the AlertDialog window, check out this article for an example.

PopupWindow in Android
Tagged on:         

47 thoughts on “PopupWindow in Android

  • February 23, 2011 at 10:16
    Permalink

    Awesome post on the PopupWindow class!

    I’ve been looking at a lot of examples and how-to’s on the web and none explained it as good as your article.

    For those needing a popup window I really recommend this post 🙂

    Cheers,
    Burt

    Reply
    • February 24, 2011 at 00:16
      Permalink

      Thx!

      Yeah I had the same problem myself at first, no real good examples of the PopupWindow that explained the entire procedure.

      /A

      Reply
  • February 28, 2011 at 02:34
    Permalink

    Great example of Android’s PopupWindow class, well worth the read!

    /Cheers

    Reply
  • March 8, 2011 at 22:36
    Permalink

    Nice writeup on the popup windows class!

    Reply
  • March 12, 2011 at 05:41
    Permalink

    Great of the popup window write! 😀

    Reply
  • April 17, 2011 at 16:26
    Permalink

    Great post about popupwindow, thanks!

    Reply
  • April 27, 2011 at 01:09
    Permalink

    Thank you very much for your post….. it helped me a lot….

    Reply
    • April 27, 2011 at 07:35
      Permalink

      Glad to hear that, good luck! 🙂

      Reply
  • April 28, 2011 at 04:28
    Permalink

    Hi! I need your help. I try to create a popup window with a Text Edit inside them. But when a touch the Text Edit, the app cash. I think that is because i dont set the context correctly.

    Reply
  • Pingback: Using AlertDialog as popup notifier

  • October 5, 2011 at 17:53
    Permalink

    Is it possible to hidden the layout under a popup window?
    my problem is: I have an image in main activity and when I click on it, the popup is shown, but I can see some part of the image that is in background, can I hide that layout? if yes, how?

    thanks

    Reply
  • November 20, 2011 at 13:10
    Permalink

    Hey,

    Thanks for this awesome post, it realy helped me alot!
    I’m trying to create a PopUp Window with a Spinner in it.
    It Pops up fine, and every button works, but when I Click on the Spinner, the App forcecloses.

    Is there anyway I can realize a Popup window including a Spinner?

    Reply
    • November 20, 2011 at 20:41
      Permalink

      Hi,

      As far as I know there shouldn’t be a problem using a Spinner on a PopUp.
      Sounds more like something might be wrong with the Spinner setup, check LogCat and see if you are getting an exception and what it says?

      Reply
  • January 23, 2012 at 10:56
    Permalink

    Thanks a lot Bro.
    I went to many blogs and tried a lot of stuff but what I really need was in your post. 🙂

    Reply
    • January 23, 2012 at 22:38
      Permalink

      Glad to have helped! 🙂

      Reply
  • December 30, 2012 at 18:16
    Permalink

    Hi, nice example.

    I would just add something i found usefull : make the view autosize.

    layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    pw = new PopupWindow(layout, layout.getMeasuredWidth(), layout.getMeasuredHeight(), true);

    Reply
    • December 30, 2012 at 19:13
      Permalink

      Thanks for the comment, I’ll add to the code as soon as I get a few minutes spare 🙂

      Happy coding!

      Reply
  • May 15, 2013 at 15:32
    Permalink

    Nice example. Thanks a lot.

    You might also add this:

    myPopup.setBackgroundDrawable(new BitmapDrawable());

    before showing the popup.
    It allows the user to close the popup via backpress

    Reply
  • July 4, 2013 at 05:16
    Permalink

    can i use this pop up window to show my 4 videos when tapped?

    Reply
  • December 6, 2014 at 13:44
    Permalink

    Hi
    I don’t know how to thank you for this, you’re great man 😉

    Reply
    • January 6, 2015 at 18:04
      Permalink

      Gee, thanks! 🙂

      Happy coding!!

      Reply
  • Pingback: Using AlertDialog as popup notifier | mobilemancer

  • January 25, 2015 at 12:14
    Permalink

    Hi, can you please tell if scrolling is possible in popup window ? I have a popup window with multiple layout ( relative layout as the main parent layout), But when i wrap it under a scroll view, the popup window is not working. Later I read in few posts stating that scroll is not possible in popup window. Please let me know. 🙂

    Reply
    • January 27, 2015 at 21:05
      Permalink

      Not sure if that is doable. Maybe some one else reading knows?

      Good luck!

      Reply
  • July 6, 2015 at 14:03
    Permalink

    Hi,.. nice tutorial. I need your help. I need to create a popup activity like the photos opening on new activity when we click on posts in facebook. But please help me to code.

    Reply
    • July 17, 2015 at 14:26
      Permalink

      Thanks!

      Coding you have to do on your own though, maybe if you got more specific questions you could get help.

      Reply
  • November 2, 2015 at 22:06
    Permalink

    (ViewGroup) findViewById(R.id.popup_element) is null

    Reply
    • November 2, 2015 at 23:41
      Permalink

      Not much to go on, but I’d gamble on popup_element not being defined properly.

      Reply
  • January 1, 2016 at 05:54
    Permalink

    would u help me how we use callout with popup window plse
    i wanted popup window with head arrow like callout in map works
    but i dont to use drawable for that…is possible to use callout with popup window

    Reply
    • January 2, 2016 at 22:58
      Permalink

      Sorry, I don’t do any Android coding any more. But maybe some other helpful reader can help you 🙂

      Good luck!

      Reply
  • February 27, 2016 at 11:00
    Permalink

    How can i use DP instead of PX?
    // create a 300px width and 470px height PopupWindow
    pw = new PopupWindow(layout, 300, 470, true);

    Reply
    • March 13, 2016 at 22:36
      Permalink

      Not sure, maybe some other user can help you?

      Happy coding!

      Reply
  • November 22, 2016 at 18:02
    Permalink

    Thanks a lot

    Reply
    • November 22, 2016 at 23:51
      Permalink

      You’re most welcome 🙂

      Reply

Leave a Reply

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