I have a Fragment with PopupWindow. I initiate Popup with following code:
private PopupWindow createPopup;
private void initiateWindow(){
try {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.window_popup,
(ViewGroup) v.findViewById(R.id.popup_element));
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
createPopup = new PopupWindow(layout, width/2 + width/4, height/3, true);
createPopup.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
It runs perfectly fine. I want to close the window whenever I click outside. This is a common thing to do, so there are plenty of tutorials and questions regarding the topic around the internet. The problem is - none of them work.
I tried using createPopup.setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
createPopup.setOutsideTouchable(true);
createPopup.setFocusable(true);
and similar answers.
I also checked whether I can fill everything behind the popup with solid color using this createPopup.setBackgroundDrawable(newColorDrawable(Color.BLACK));
, to verify whether I've got wrong core code, but it didn't help either - everything not related to popup layout remained visible.