Android PopupWindow and WRAP_CONTENT don't work together

30,060

Solution 1

Simply changed the way you create it by:

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

Solution 2

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);

Solution 3

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.

Solution 4

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.

Solution 5

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);
Share:
30,060
Boris
Author by

Boris

Updated on October 10, 2020

Comments

  • Boris
    Boris over 3 years

    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?

  • Stevey
    Stevey about 7 years
    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.
  • Kenny
    Kenny about 7 years
    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.
  • Jonny Right
    Jonny Right about 6 years
    This is the only solution which works in my case. Thx
  • webrama.pl
    webrama.pl over 5 years
    But required API >= 19
  • DIRTY DAVE
    DIRTY DAVE over 4 years
    Easy solution for me. Changed my relative layout to linear and it's fixed