Reputation: 123
I am trying to create an app that will run on both PC (Windows) and Android, however, I am having issues getting the correct screen size on both.
On Windows, the Screen.getBounds() method seems to always return the correct full screen size (i.e. the screen without the taskbar space etc). On Android, using Screen.getBounds() will return a screen size which is much larger than the actual screen size. The only way I can get my app to work correctly on android is to use Screen.getVisualBounds(). However, on Windows, using Screen.getVisualBounds() always returns a size slightly smaller in height than the actual total screen size since it removes the space occupied by the taskbar.
Does anyone know why the Screen.getBounds() returns a much higher value on Android than the actual visible screen?
Thanks.
Upvotes: 1
Views: 1265
Reputation: 45456
Screen.getBounds()
returns the physical pixels, while Screen.getVisualBounds()
returns the logical ones.
While on desktop the difference between those bounds is just related to the presence of the task bar, on mobile device the difference is related to the pixel density or scale, and it can be greater than 1.
This is what those methods return on a Nexus 6:
due to the fact that the pixel density for this device is 3.5.
Back to your initial problem, you will need to use Screen.getVisualBounds()
on Android.
But for Desktop, you are free to select the size:
@Override
public void start(Stage stage) {
Rectangle2D bounds = JavaFXPlatform.isDesktop() ?
Screen.getPrimary().getBounds() :
Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(new StackPane(), bounds.getWidth(), bounds.getHeight());
stage.setScene(scene);
stage.show();
}
where JavaFXPlatform
comes from Gluon Charm Down, an OSS library that you can get here.
Upvotes: 2