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?

1
  • 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
    Commented Apr 18, 2013 at 14:34

9 Answers 9

35

Simply changed the way you create it by:

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
1
  • 4
    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
    Commented Mar 17, 2017 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);
1
  • But required API >= 19
    – webrama.pl
    Commented Sep 26, 2018 at 13:19
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.

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.

1
  • 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
    Commented Mar 17, 2017 at 6: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);
1
  • Easy solution for me. Changed my relative layout to linear and it's fixed
    – DIRTY DAVE
    Commented Jan 15, 2020 at 16:27
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..

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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.