Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:20:16+00:00 2026-05-26T22:20:16+00:00

I’m currently trying to position a popup window dynamically on screen based on an

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:20:16+00:00Added an answer on May 26, 2026 at 10:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.