androidtogglepopupwindowdismiss

Android PopupWindow prevent dissmis on anchor click


I have a popupWindow that showAsDropDown on button click and this popupWindow has setOutsideTouchable(true) and I want to toggle show the popup when I click on my button but also to dismiss when I click outside of popup. The problem is that outside touch is called before button click and my popup is hide and show after that. Is there a way to do that ?

FIXED!!!

I figured out that the problem was that my popupwindow was not focused and I have to call

popupWindow.setFocusable(true);

to fix it.


Solution

  • Please check this out.

    If i understand in a correct way,

    // Pop up Window showing
    
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        popupView = layoutInflater.inflate(R.layout.menu_popup, null);
        popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
    
     // Add setBackgroundDrawable to the pop up window to close the dialog :
    
            popupWindow.setBackgroundDrawable(getResources().getDrawable(
                android.R.color.transparent));
        popupWindow.setTouchable(true);
    

    using the following check, you can show the pop up as you desired

    findViewById(R.id.topMenu).setOnClickListener(
                new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
    
                            popupWindow
                                    .showAsDropDown(findViewById(R.id.topMenu));
    
                });
    

    I have checked this. It is working fine.