51

I looked online and was not able to find a working example of the PopupWindow class. The code examples I found online either compile but do not work, or are using methods which have since been removed (such as Activity.getViewInflate()).

Is there a simple working example that displays a PopupWindow?

frogatto
  • 28,539
  • 11
  • 83
  • 129
Todd
  • 1,906
  • 2
  • 16
  • 21

3 Answers3

78

I created a working example based on this Google Groups post.

To create a simple working PopupWindow, you'll need to do the following:

  1. Create a layout XML which describes the View that will be rendered within the PopupWindow.
  2. Invoke the PopupWindow by inflating the layout XML, and assign the appropriate "parent view" to the pop-up.

popup_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Test Pop-Up"
    />

</LinearLayout>

Java code:

    LayoutInflater inflater = (LayoutInflater)
       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    PopupWindow pw = new PopupWindow(
       inflater.inflate(R.layout.popup_example, null, false), 
       100, 
       100, 
       true);
    // The code below assumes that the root container has an id called 'main'
    pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0); 
Todd
  • 1,906
  • 2
  • 16
  • 21
  • 3
    when I try your example code, it seems like it's working, but it's really weird. The popup must be coming up, but it's almost as if it's nearly completely transparent. I can see my TextView's ghost over one of the main GUI buttons, but the EditText and Button are completely invisible. I looked over the function signatures and couldn't find anything regarding opacity. Can you think of anything that might cause this behavior? – Dave Dec 11 '10 at 05:40
  • Sorry Dave, I haven't touched Android in quite awhile so my memories of this particular piece of code has faded. I found this SO question regarding opacity / alpha. Hope it helps: http://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android – Todd Dec 15 '10 at 18:53
  • Why would this.getSystemService not exist? – ingh.am Jan 07 '11 at 15:11
  • this.getSystemService not exits; because "this" is a class without Activity – LTEHUB Jun 16 '11 at 10:27
  • 15
    The reason the popup is completely transparent is answered in the javadoc for PopupWindow: `The popup does not provide any background. This should be handled by the content view.`. Thus your popup view layout root should specify an android:background="" attribute. – Chris Knight Aug 25 '11 at 20:02
  • 4
    Can you explain the meaning of "root container"? is it the view where the popup starts from? – Guy Jan 02 '12 at 14:00
  • @ing0 if you are inside a fragment you need to do this: `getActivity().getSystemService` – Adil Malik Feb 05 '13 at 17:53
  • @Todd, I am also learning popup window, do you know how to set the popup window's height automatically fitting the wrapped content instead of assign a magic value to it ? – Flybywind Feb 08 '13 at 10:38
  • i use this popUpWindow. window pop Up when a button is clicked. But the problem is that after pop up the application get stuck. – Vikky Feb 26 '13 at 09:56
  • Notice that in the PopupWindow constructor, the last parameter is a boolean... you'll want to set this to true to capture touch events on your popup view. See https://developer.android.com/reference/android/widget/PopupWindow.html#PopupWindow(android.view.View, int, int, boolean) – Luis Feb 13 '14 at 04:21
  • 2
    what does the term `root container` mean ? – gkmohit Jun 18 '15 at 01:32
  • Figured out that root container is the id of the root element of the parent view – Daniel Kobe Sep 30 '15 at 01:53
4

AFAIK only the AbsoluteLayout works(pls confirm), as seen on http://sree.cc/google/android/android-popup-window . I've shown the popup right, but LinearLayout was not showing all elements. But AbsoluteLayout is deprecated!

FrameLayout also works, but organizing views is a nightmare since the official documentation says it is only good for holding one view.

Also, to be able to receive touch events, you need to do this: setBackgroundDrawable(new BitmapDrawable());

as further explained at Android popup window dismissal

Community
  • 1
  • 1
Ravindranath Akila
  • 209
  • 3
  • 35
  • 45
-1

You are getting the invisibility because you didn't set the Background color of the layout to whom you are inflated.set it as android:background="#778899",and definitely you can see the things

raman
  • 337
  • 3
  • 13