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?
Use setWidth(int) and setHeight(int) . Set the layout type for this window. Display the content view in a popup window anchored to the bottom-left corner of the anchor view. Displays the content view in a popup window anchored to the corner of another view.
It is created inside the res/menu directory.
I created a working example based on this Google Groups post.
To create a simple working PopupWindow, you'll need to do the following:
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With