0

How do I dismiss PopupWindow when I tap outside?

This is my code:

private void initiatePopupWindow(View viewButton) {
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popup_layout, null);
        mPopupWindow = new PopupWindow(this);
        mPopupWindow.setContentView(view);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.showAsDropDown(viewButton);
//        final PopupWindow pw = new PopupWindow(inflater.inflate(
//                R.layout.popup_layout, null, false), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
//        pw.showAtLocation(view, Gravity.BOTTOM, 0, view.getHeight());
        ListView moreOptions = (ListView) mPopupWindow.getContentView().findViewById(R.id.list_view);
        String[] options = new String[]{"Settings", "Feedback", "Contribute", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial"};

        final ArrayList<String> moreOptionsList = new ArrayList<String>();
        for (int i = 0; i < options.length; ++i) {
            moreOptionsList.add(options[i]);
        }
        ArrayAdapter<String> moreOptionsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                moreOptionsList);
        moreOptions.setAdapter(moreOptionsAdapter);
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setOutsideTouchable(true);
//        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
//                    mPopupWindow.dismiss();
//                }
//                return false;
//            }
//        });
    }

I follow:

mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
mPopupWindow.setOutsideTouchable(true);

But it not working. How do I resolve this ?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • i resolve this by put showAsDropDown(viewButton) under mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), "")); mPopupWindow.setOutsideTouchable(true); – JackieChung Nov 09 '17 at 01:28

1 Answers1

0

This is work in my code, try this :

mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
mPopupWindow.setOutsideTouchable(true);

Happy coding!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49