Skip to content

Instantly share code, notes, and snippets.

@jmif
Created November 17, 2011 17:24
Show Gist options
  • Save jmif/1373818 to your computer and use it in GitHub Desktop.
Save jmif/1373818 to your computer and use it in GitHub Desktop.
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect =
new Rect(location[0], location[1], location[0] + this.anchor.getWidth(), location[1]
+ this.anchor.getHeight());
this.anchorRectangle = anchorRect;
this.root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.root.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int xPos, yPos;
// Align popup with right side if the root is wider than anchor
// Align with left otherwise
if (rootWidth > anchorRect.right - anchorRect.left) {
xPos = anchorRect.right - rootWidth;
} else {
xPos = anchorRect.left + 15;
}
// Correct for spilling off the edge
if (xPos + rootWidth > screenWidth)
xPos = screenWidth - rootWidth - 20;
// xPos = ((screenWidth - rootWidth) / 2) + xOffset;
yPos = anchorRect.top - rootHeight + yOffset;
this.rootRectangle = new Rect(xPos, yPos, xPos + rootWidth, yPos + rootHeight);
boolean onTop = true;
// display on bottom
if(rootHeight > anchorRect.top) {
onTop = false;
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.anim.grow_from_top);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment