why does the screen size appears bigger than it really is android studio

Viewed 72

I'm confused I am stuck for days and have no idea what to do also searched for answers for a long time but nothing so far...

so anyway i'm trying to create a little 2D game (On landscape) from above perspective everything works find there is just one problem with the screen size. I tried to get size of the screen and it worked just fine the problem is that even though the size I get is the right size(Checked it on a few devices) when I ran the app it appears like the screen is bigger than that. On some devices it looks fine but on some it shows like they are bigger than their actual size.

the Y axis is wrong The X axis looks fine just as it supposed to be

    Point screenSize=new Point();
    getWindowManager().getDefaultDisplay().getSize(screenSize);
    Display display=getWindowManager().getDefaultDisplay();
    display.getRealSize(screenSize);

This is the code I use to get the device size.

Please if you have any idea what the problem might be help me

1 Answers

I'm not sure what API version you are on, but both getDefaultDisplay() and getRealSize() are deprecated in favor of WindowManager#getCurrentWindowMetrics(). However, a screen can have insets, due to parts of the screen that are not accessible to your app, and scaling on high DPI screens. The getRealSize() documentation has these caveats:

The returned size will fall into one of these scenarios:

  1. The device has no partitions on the display. The returned value is the largest region of the display accessible to an app in the current system state, regardless of windowing mode.
  2. The device divides a single display into multiple partitions. An application is restricted to a portion of the display. This is common in devices where the display changes size, such as foldables or large screens. The returned size will match the portion of the display the application is restricted to.
  3. The window manager is emulating a different display size, using adb shell wm size. The returned size will match the emulated display size.

The returned value is unsuitable to use when sizing and placing UI elements, since it does not reflect the application window size in any of these scenarios.

It seems it would be much better to use WindowMetrics instead.

Related