PopupWindow in Android

Using the PopupWindow class in Android apps

Example of an Android PopupWindowI 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.

32 comments

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>