I know this late, but I notice that people still have a popup problem. I decided to write a fully working example in which you can close the pop-up window by touching or clicking it or simply touching the window itself. To do this, create a new PopupWindow class and copy this code:
Popupwindow.class
public class PopupWindow extends android.widget.PopupWindow { Context ctx; Button btnDismiss; TextView lblText; View popupView; public PopupWindow(Context context) { super(context); ctx = context; popupView = LayoutInflater.from(context).inflate(R.layout.popup, null); setContentView(popupView); btnDismiss = (Button)popupView.findViewById(R.id.btn_dismiss); lblText = (TextView)popupView.findViewById(R.id.text); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); setWidth(WindowManager.LayoutParams.WRAP_CONTENT); // Closes the popup window when touch outside of it - when looses focus setOutsideTouchable(true); setFocusable(true); // Removes default black background setBackgroundDrawable(new BitmapDrawable()); btnDismiss.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { dismiss(); }}); // Closes the popup window when touch it /* this.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { dismiss(); } return true; } }); */ } // End constructor // Attaches the view to its parent anchor-view at position x and y public void show(View anchor, int x, int y) { showAtLocation(anchor, Gravity.CENTER, x, y); } }
Now create a popup layout: popup.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="PopupWindow Example" android:textColor="#000000" android:textSize="17sp" android:textStyle="italic" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical"> <Button android:id="@+id/btn_dismiss" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dismiss" android:visibility="gone" /> <TextView android:id="@+id/lbl_dismiss" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Touch outside of this box to dismiss" android:textColor="#ffffff" android:textStyle="bold" /> </FrameLayout>
In your main action, instantiate the PopupWindow class:
final PopupWindow popupWindow = new PopupWindow(this); popupWindow.show(findViewById(R.id.YOUR_MAIN_LAYOUT), 0, -250);
where YOUR_MAIN_LAYOUT is the layout of the current activity in which a popup window pops up
Marcin S. Sep 29 '12 at 19:01 2012-09-29 19:01
source share