I'm trying to create a kind of popupMenuItem with a popupWindow and a custom layout.
I have a button which shows the popupWindow when I click on it. And I would like to trigger and event to dismiss this popupWindow when I click on this button one more time or when I click outside of the popupWindow.
But at the moment it doesn't work, my setTouchInterceptor is not triggered, have you got an idea to solve this problem ?
Each time this popupWindow is open, I can't have access to all the others UI elements even with a setOutsideTouchable(true).
Here my code:
- popupMenuImageView is my dropdown button (imageview actually) which shows my popup window
popupWindow my global variable for my PopupWindod object.
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView); popupMenuImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (popupWindow == null) { int width = 375; int height = 240; TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id); LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout); popupWindow = new PopupWindow(layout, width, height, true); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ popupWindow.dismiss(); return true; } return false; } }); } popupWindow.showAsDropDown(popupMenuImageView); } });
Also I've tried this which didn't work too:
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);
View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){
@Override
public void onClick(View v) {
if (isShowing) {
popupWindow.showAsDropDown(popupMenuImageView);
isShowing = false;
}else{
popupWindow.dismiss();
}
}
};
private void createMenuItem(){
if (popupWindow == null) { int width = 375; int height = 240; TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id); LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout); popupWindow = new PopupWindow(layout, width, height, true); popupWindow.setOutsideTouchable(false); popupWindow.setFocusable(true); }