Deepak Rattan
Deepak Rattan

Reputation: 1299

Popup window is not dismissed after clicking outside it

I am using the following code to create popup window below toolbar:

private void initializePopUpWindow() {
    //inflate the popupwindow_attachment.xml
    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
    LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
    popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

    //Displaying the popup at a specific location
    //popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150);
    popupWindow.showAsDropDown(toolbar,0,0);

    //Close the popup when touch outside
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);              
}

I have successfully added a popup window below toolbar ,but it is not removed when i am clicking outside it .No functionality is working .Please help me to fix the issue .

Edited Working Code :

I was doing some foolish mistakes.I fixed it now.

1. onCreate() method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_chat);

    //Toolbar
    toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat);
    toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar
    setSupportActionBar(toolbar);

    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
    LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
    popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

    //Close the popup when touch outside
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

2. onoptionsItemSelected()

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_viewContacts:
            return true;
        case R.id.action_media:
            return true;
        case R.id.action_search:
            return true;
        case R.id.action_block:
            return true;
        case R.id.action_email_chat:
            return true;
        case R.id.action_clear_chat:
            return true;
        case R.id.action_attach:
            initializePopUpWindow();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

3. initializePopUpWindow() method:

private void initializePopUpWindow() {
    popupWindow.showAsDropDown(toolbar, 0, 0);
}

Here i have initialized the popupWindow in onCraeteMethod() of the activity.Also the code that handles the closing of popup window is added in onCreate() method.I have called the code that is creating the popupwindow in onOptionsItemSelected() method.Now its working for me.Thanks for the help .

Upvotes: 1

Views: 1690

Answers (3)

Harshad
Harshad

Reputation: 1344

Please try to set setBackgroundDrawable on PopupWindow that should close the window if you touch outside of it.

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setTouchInterceptor(new OnTouchListener() { // or whatever you want
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_OUTSIDE) { // here I want to close the popupWindow when clicking outside it but at all this is just an example of how it works and you can implement the onTouch() or the onKey() you want
            popupWindow.dismiss();
            return true;
        }

        return false;
   }
});

Upvotes: 1

Chandan kushwaha
Chandan kushwaha

Reputation: 951

Add these two lines. It will do your work.

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

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317467

The method setOutsideTouchable doesn't make your popup dismiss on touch outside. It merely allows the popup to receive touch events that happen outside it. I think you have will to register a touch listener with setTouchInterceptor which will then call dismiss() on the window.

Upvotes: 0

Related Questions