J VR
J VR

Reputation: 61

android PopupMenu showAtLocation y offset

Does anyone know why when I use this code:

popupMenu.showAtLocation(containerView, Gravity.BOTTOM|Gravity.LEFT, x_offset, y_offset);

The y offset doesn't actually move the popup until it's greater than a certain number (in this case approx 120). For values under 120, nothing happens; for values over 120, the popup moves vertically up.

I'm guessing it's to do with the size of the popup, for which there doesn't seem to be a way to actually get the dimensions of to compensate for this threshold.

I'd like to know because a) there's scant information about this method, and b) I can't seem to get it to work to position my popup correctly.

Upvotes: 1

Views: 1292

Answers (2)

Roma Heshten
Roma Heshten

Reputation: 46

Pretty old question but I would like to mention here that in case if anyone else is wondering what is the threshold for the yOffset - it is the top insets of the view you are passing to the showAtLocation() method. In most cases this is your system bar height.

You can try to calculate the yOffset by

int desiredOffsetPx = 250;
int yOffset = desiredOffsetPx + systemWindowInsetTop
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, yOffset)

In order to get systemWindowInsetTop please check this answer

Upvotes: 1

Ankit Saini
Ankit Saini

Reputation: 384

void showAtLocation (View parent, 
            int gravity, 
            int x, 
            int y)

Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP.

You can find whole detail on this link : deveoper.android.com

Upvotes: 0

Related Questions