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 6579283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:44+00:00 2026-05-25T15:52:44+00:00

I have some popups on my app, it’s fullscreen and the following code: content.setLayoutParams(new

  • 0

I have some popups on my app, it’s fullscreen and the following code:

    content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    content.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    int screenWidth = windowManager.getDefaultDisplay().getWidth();
    int screenHeight = windowManager.getDefaultDisplay().getHeight();
    int x = screenWidth / 2 - content.getMeasuredWidth() / 2;
    int y = screenHeight / 2 - content.getMeasuredHeight() / 2;
    window.showAtLocation(content, Gravity.NO_GRAVITY, x, y);

Make the window show centered.

But I have another Activity which is not fullscreen, when the popup opens it’s right-down from where it’s need to be.

Trying to figure out why this happens, I think the showAtLocation shows it’s relative to the current Activity, but I need to show it relative to the display.

How can I do that? Or there’s a simpler way to just the popup just centered?

  • 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-25T15:52:45+00:00Added an answer on May 25, 2026 at 3:52 pm
    popupWindow.showAtLocation(anyViewOnlyNeededForWindowToken, Gravity.CENTER, 0, 0);
    

    This will center your view.

    It took me 2 hours of pain to figure this out. I didn’t need any black magic maths to handle this.

    The view is only needed for the window token, it has no other impact on the location.

    Gravity tells the layout manager where to start the coordinate system and how to treat those coordinates. I can’t find the docs but hacking is showing me that:

    • CENTER uses the middle of the popup to be aligned to the x,y specified. So 0,0 is screen centered
    • BOTTOM uses the bottom of the popup to be aligned to the x,y specified. So 0,0 has the popup bottom aligned with the screen bottom. If you want 10px padding then y=10 (not -10) to move the popup up the screen 10 pixels.

    I wrote this up over here Android PopupWindow.showAtLocation() and effects of Gravity

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

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
I have some code that popups a message box: MessageBox.Show(this, You have not inputted
In my iPhone app I have a UIScrollView with custom content views. At some
I have a modal popup that initially shows some content but expands a div
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some C# / asp.net code I inherited which has a textbox which
I'm developing a phone app and need a modal dialog with some rich content
I have some simple jQuery that renders a page in my Rails app. Here's
I have some C# 4.0 code that attempts to install a CA (.der encoded)
I have some Popups on my screen, and need something not so common. I

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.