Is there a simple example of the PopupWindow class using Android v2.0?
Asked Answered
M

3

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?

Mackey answered 28/12, 2009 at 3:37 Comment(0)
M
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); 
Mackey answered 28/12, 2009 at 3:53 Comment(12)
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?Creasy
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: #2839257Mackey
Why would this.getSystemService not exist?Panamerican
this.getSystemService not exits; because "this" is a class without ActivityOzzy
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.Armistead
Can you explain the meaning of "root container"? is it the view where the popup starts from?Vagarious
@ing0 if you are inside a fragment you need to do this: getActivity().getSystemServiceJacklin
@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 ?Queston
i use this popUpWindow. window pop Up when a button is clicked. But the problem is that after pop up the application get stuck.Brune
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 developer.android.com/reference/android/widget/…, int, int, boolean)Tsui
what does the term root container mean ?Abbe
Figured out that root container is the id of the root element of the parent viewOrdonnance
C
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

Compendium answered 27/8, 2011 at 3:3 Comment(0)
B
-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

Blaspheme answered 12/12, 2011 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.