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?
Simply changed the way you create it by:
PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With