4

I wanted to dismiss the PopupWindow when touch outside, I got that answer from this SO question.

here is that Link

Here they asked to inlude these two line of code.

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
myPopupWindow.setOutsideTouchable(true);

Now the Popup disappers when i touch outside of PopupWindow.

setOutsideTouchable(true); alone is not working. when i set the background Drawable,it is working. How this magic happens ? can anyone explain on this?

Also new BitmapDrawable() is deprecated. is there any alternative for this ?

Community
  • 1
  • 1
saravanan
  • 5,339
  • 7
  • 45
  • 52

3 Answers3

0

Try code below:

myPopupWindow.setCanceledOnTouchOutside(true);
myPopupWindow.setCancelable(true);
The Heist
  • 1,444
  • 1
  • 16
  • 32
  • 1
    I am using PopupWindow not Dialog. Both methods are undefined for PopupWindow. – saravanan Feb 08 '14 at 06:19
  • I've created `popupWindow` as below: `PopupWindow popup = new PopupWindow(mContext); popup.setContentView(layoutPopup); popup.setWidth(850); popup.setHeight(550); popup.setFocusable(true); // Clear the default translucent background popup.setBackgroundDrawable(view.getResources().getDrawable( R.drawable.white_color_patch));` This works for me perfectly. – The Heist Feb 08 '14 at 06:28
  • So, alternative is `popUp.setFocusable(true);` – The Heist Feb 08 '14 at 06:34
  • popUp.setFocusable(true); is working only with setBackgroundDrawable. but i dont know what magic happens when i set backgroundDrawable. – saravanan Feb 08 '14 at 06:59
0

Use TouchInterceptor to dismiss the popup:-

private LayoutInflater inflater;
private PopupWindow pw;
private View popupView;

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.popup_layout, null, true);
pw = new PopupWindow(popupView,750,500,true);
pw.setBackgroundDrawable(new BitmapDrawable());

pw.setTouchInterceptor(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                    pw.dismiss();

                    return true;
                }
        return false;
    }
});

pw.showAtLocation(findViewById(R.id.main_layout),Gravity.BOTTOM, 3, 35);
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • k. can you post your full code of the popup window. – Yugesh Feb 08 '14 at 06:26
  • final PopupWindow addToOrderDialog = new PopupWindow(layout,android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT); addToOrderDialog.setContentView(layout); addToOrderDialog.setBackgroundDrawable(new BitmapDrawable()); addToOrderDialog.setOutsideTouchable(true); – saravanan Feb 08 '14 at 06:34
  • @saravanan see my edited answer, this also not working. Revert back. – Yugesh Feb 08 '14 at 06:46
0

Make your popup in full screen transparent "root" clickable layout and then add your popup there with dimensions (popup.setWidth(850); popup.setHeight(550)). Also make root transparent layout clickable so you will know that user touches "outside" cuz for user the only popup will be visible. This is the root layout:

final LinearLayout contentLayout = new LinearLayout(activity);
contentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
contentLayout.setOrientation(LinearLayout.VERTICAL);
contentLayout.setClickable(true);
contentLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dismissPopup();
    }
});

now you only need to add your popup into that layout. Why this way is any better? Cuz you could set contentLayout background color to achieve effect of making entire screen shadowed by the popup so it will be visible to user that only popup is modal and active right now.

Stan
  • 6,511
  • 8
  • 55
  • 87