I want to close a popup menu on clicking a imageview.But it is not working.

menuicon.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (f == 1) {
            f = 0;
            mPopupMenu.setContentWidth(ContactsActivity.this.getWindowManager()
                .getDefaultDisplay()
                .getWidth() / 2);
            mAdapter.notifyDataSetChanged();
            mPopupMenu.setAnchorView(menuicon);
            mPopupMenu.show();

        } else {
            mPopupMenu.dismiss();
            f = 1;

        }

    }
});

When I click the menuicon first time the popup menu is displaying correctly.But when i click for the 2nd time the menu is not closing.I debugged my code and found that the imageview is actually not firing roe 2nd time.menuicon is an imageview.And here is xml code:

<ImageView
            android:id="@+id/menuicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/menuicon"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp" 

        />       

Initial value of f is 1.

有帮助吗?

解决方案

You need to call setModal(false) of mPopupMenu. It will call setFocusable of inner PopupWindow and allow you to get events in the background window.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top