How do I compute the width and height of an inflated View if the parent is a PopupWindow, not a ViewGroup? I cannot use LayoutInflator.inflate(int resId, ViewGroup parent, attachToRoot boolean) because PopupWindow is not a ViewGroup, so I use LayoutInflator.inflate(int resId) instead, but after that I getWidth() and getHeight() return zero 🙁
I need to resize the PopupWindow to fit the View, but cannot do so until the View has a parent. Do I have a chicken-and-egg problem?
By the way the View is a subclass of RelativeView so calculating it manually is essentially out of the question.
Thanks in advance,
Barry
Actually popupWindow supports “wrap content” constans, so if you want popup be exact as your view – use this:
—other options—
getWidth()andgetHeight()returns zero, because view have size only after drawing on the screen. You can try to get values fromLayoutParamsof inflated view.If there is
fill_parentvalue – you are in egg-chicken situation.If there is values in
pxordpyou can manually calculate size in pixels:If there is
wrap_contentvalue – you can useview.measure()method – all children and view itself will be measured, and you can getview.getMeasuredHeight()andview.getMeasuredWidth().