23

I open a popu window like this:

mInfoPopup = new PopupWindow(layout, 400, 600, true);
mInfoPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

The window then gets the exact size specified (400x600) and does NOT adjust its size to its content. What do I need to change so that the popup window will actually wrap around its contents?

Boris
  • 8,551
  • 25
  • 67
  • 120
  • **From the docs** If the popup is showing, calling this method will take effect only the next time the popup is shown. is this your problem? – Sankar V Apr 18 '13 at 14:34

9 Answers9

35

Simply changed the way you create it by:

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Gomino
  • 12,127
  • 4
  • 40
  • 49
  • 3
    This doesn't work in the case where the content is larger than the allowable space under or above the anchor view. Or if the content isn't measured until later. – Kenny Mar 17 '17 at 18:25
14
PopupWindow popupWin = new PopupWindow(mContext);
// WRAP_CONTENT works well for height, but not for width.
popupWin .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 
// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth()); 
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);
Arst
  • 3,098
  • 1
  • 35
  • 42
8

I found a solution that works for me (I am working with API 10), you can use it:

popupWindow.setWindowLayoutMode(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);

If you don't set height/width or set 0 it won't work. Example:

...
private Button button;

protected void onCreate(Bundle savedInstanceState) {
    ...
    attached_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {                
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup_layout, null);

            final PopupWindow popupWindow = new PopupWindow(getApplicationContext());
            popupWindow.setContentView(popupView);
            popupWindow.setWindowLayoutMode(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(1);
            popupWindow.setWidth(1);
            popupWindow.setFocusable(true);

            Button dismiss = (Button) popupView
                    .findViewById(R.id.dismiss);

            dismiss.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(button);

        }
    });

I hope to help you.

vgc
  • 115
  • 1
  • 7
5

Following code worked for me.

LayoutInflater layoutInflater = LayoutInflater.from(context);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setHeight(ListPopupWindow.WRAP_CONTENT);

Here, used popup.setWidth(ListPopupWindow.WRAP_CONTENT) instead of popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT) in my case.

Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • 1
    This worked for me as well. It shouldn't work, since ListPopupWindow has nothing to do with PopupWindow. I think we are just getting lucky -- the value of the constant is -2, which gives the correct behavior, whereas ViewGroup.LayoutParams.WRAP_CONTENT's value is -1. – Stevey Mar 17 '17 at 06:04
3

don't use RelativeLayout in layout, replace LinearLayout, maybe it's working.

popview = LayoutInflater.from(context).inflate(R.layout.pop_citypicker, null);

popwindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
qinmiao
  • 5,559
  • 5
  • 36
  • 39
1

Below code is use-full for decreasing layout width and height pop-window

and i used in that custom layout with aligned center

  final PopupWindow popupWindow = new PopupWindow(getActivity());
            PopupMenu popup = new PopupMenu(getActivity(), v, Gravity.CENTER);
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.popmenu1t1, null);
            l1 = (LinearLayout) view.findViewById(R.id.atoz);
            l2 = (LinearLayout) view.findViewById(R.id.ztoa);
            l3 = (LinearLayout) view.findViewById(R.id.phtol);
            l4 = (LinearLayout) view.findViewById(R.id.pltoh);
            l5 = (LinearLayout) view.findViewById(R.id.dhtol);
            l6 = (LinearLayout) view.findViewById(R.id.dltoh);
            l7 = (LinearLayout) view.findViewById(R.id.dotor);
            l8 = (LinearLayout) view.findViewById(R.id.drtoo);
            int width = 900;
            int height = 400;
            try {
                WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
                width = wm.getDefaultDisplay().getWidth();
                height = wm.getDefaultDisplay().getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }

            popupWindow.setWidth(width*3/6);
            popupWindow.setHeight(height*3/8);
            popupWindow.setFocusable(true);

            popupWindow.setContentView(view);
            popupWindow.setBackgroundDrawable(null);
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

These two lines not use full for now

popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                 popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

Happy coding friends..

Venkatesh
  • 1,034
  • 13
  • 25
0
 final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
    newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

This worked for me, we need the width/height to be initialized at 1 to make the wrap content work.

iko_wp
  • 739
  • 6
  • 9
0

I had a similar problem but even setting the size in new PopupWindow(...) wasn't working.

I had an image as background of the popup and I solved putting it as background of the layout (I'm using the new ConstraintLayout) with android:background. The image is a 9-Patch image so it resize in a nice way.

Paolo Godino
  • 21
  • 1
  • 4
0

For me it depended strangely on what did I pass as a contentView to the PopupWindow constructor:

new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

When it was a LinearLayout without child views and a fixed width and height, passing the width=height=WRAP_CONTENT to the PopupWindow constructor did not work - I got 0x0 size.

When I added a child to that light the width=WRAP_CONTENT started working as expected, but the height=WRAP_CONTENT was working more like MATCH_PARENT, taking the full height of the screen.

After I changed the contentView to a FrameLayout or CardView it started working with width=height=WRAP_CONTENT properly.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45