Hazem Hagrass
Hazem Hagrass

Reputation: 9848

Why android popupwindow showAsDropDown offsetx is not effective?

popupWindow.showAsDropDown(morebutton, xOffset, yOffset);

No matter the value of xOffset, the popup is on the right side of the screen display

final PopupWindow popupWindow = new PopupWindow(DashboardActivity.applicationContext);
                        LayoutInflater inflater = (LayoutInflater) DashboardActivity.applicationContext.getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
popupWindow.setFocusable(true);
                        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                        popupWindow.setContentView(view);
                        popupWindow.setBackgroundDrawable(new ColorDrawable(
                                android.graphics.Color.TRANSPARENT));
popupWindow.showAsDropDown(morebutton, **-220**, -40);

No matter what I set the value offsetX, he is on the right side of the screen display

Upvotes: 6

Views: 5277

Answers (1)

Markus Rubey
Markus Rubey

Reputation: 5238

PopupWindow tries to align your contentView to your anchor's Gravity.TOP | Gravity.START (top left corner) by default. If there is no space left for your contentView it gets offset to fit on the screen, which is probably why it stays on your right side.

The following xOffset will align the contentView at your anchor's top right corner:

view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int xOffset = -(view.getMeasuredWidth() - morebutton.getWidth());

popupWindow.showAsDropDown(morebutton, xOffset, 0);

You might want something like:

popupWindow.showAsDropDown(morebutton, xOffset - 220, -40);

Upvotes: 8

Related Questions