Dimiss popup when touching outside

I wanted to fire PopupWindowwhen I touched outside, I got this answer from this SO question.

here's the link

Here they asked to include these two lines of code.

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

Now the popup disappears when I touch the outside PopupWindow.

setOutsideTouchable(true);does not work. when I set the background to Drawable, it works. How does this magic happen? can anyone explain this?

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

+4
source share
3 answers

Try the code below:

myPopupWindow.setCanceledOnTouchOutside(true);
myPopupWindow.setCancelable(true);
0
source

Use TouchInterceptor to close 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);
0
source

"" , (popup.setWidth(850); popup.setHeight(550)). root , , "", . :

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();
    }
});

. ? contentLayout , , , .

0

Source: https://habr.com/ru/post/1525908/


All Articles