1

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.

Community
  • 1
  • 1
Oleksandr Firsov
  • 1,428
  • 1
  • 21
  • 48

2 Answers2

0

Closes the popup window when touch outside of it - when looses focus

createPopup.setOutsideTouchable(true);
createPopup.setFocusable(true);

Removes default black background

createPopup.setBackgroundDrawable(new ShapeDrawable());

if ShapeDrawable() didt work use this one

createPopup.setBackgroundDrawable(new BitmapDrawable());

Same thread

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

Create a new xml style as shown below.

<style name="AppTheme.PopupWindow">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

All you need to do after creating the theme is apply it to the fragmentactivity in the manifest.

<activity android:name=".PopupWindow" android:theme="@style/AppTheme.PopupWindow"></activity>
itsBGO
  • 49
  • 1
  • 6