3

I'm currently trying to position a popup window dynamically on screen based on an anchor element. For some reason, when I position this element it appears about 100px to the left of where I want it. For instance, if I position it on an anchor near the edge of the screen the popup window should appear with its right edge just to the left of the anchors right edge. However, the popup window appears flush to the edge of the screen. If I subtract 100 from my calculate xPos then it works fine.

I have checked my math and have stepped through the display code to check that the values are not coming out incorrectly, so I'm not sure what is causing the problem. I'm just learning views in android though, so I'm sure I am overlooking something. Hopefully someone here will know!

I've posted the display code below, and would be happy to post anything else if needed.

Thanks in advance!

    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);
jmif
  • 1,192
  • 2
  • 14
  • 22
  • I stepped through the code to get some numbers in hopes that it will help: screenWidth = 1280 xPos = 765 rootWidth = 485 (width of the view of the popup window) So it seems like I should have 30px left over at the end, but the popup window appears flush to the right edge of the screen – jmif Nov 17 '11 at 20:10

2 Answers2

3

Finally figured out what the problem was. In the code above I use .measure() and then .getMeasuredWidth/Height() to get the measurements of the view that I am trying to position. Unfortunately these were actually less than the values of getWidth() and getHeight() after the view had been displayed. I ended up overriding onSizeChanged so that I could keep track of the size after the view was displayed. Once the view was displayed, I grab the correct width/height and display the popup in its correct postion immediately.

Hope this helps someone else, though I'm not sure why getMeasuredWidth/Height were returning smaller values.

jmif
  • 1,192
  • 2
  • 14
  • 22
1

Can you check whether the returned value of this.windowManager.getDefaultDisplay().getWidth(); is correct?

If not, it might be a similar issue as in Android: Showing wrong screen resolution

Try adding

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

to your manifest file.

Community
  • 1
  • 1
Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • Yup I'll check that when I get back to the device, I'm assuming that the returned value should be equal to the resolution of the device? – jmif Nov 17 '11 at 18:25
  • Just checked the code and I do have a min sdk version in there, is the target version required for this as well? – jmif Nov 17 '11 at 18:33