I am writing a JavaFX application for Microsoft Surface and I am looking for a way to detect a screen orientation change when rotating the device from portrait to landscape and vice versa. The current method I am using to detect the screen orientation is as follows:
window.widthProperty().addListener((obs, oldVal, newVal) -> {
if((double) newVal < window.getHeight()) {
setPortraitMode();
} else {
setLandscapeMode();
}
});
This method works fine for manual window resizing. However, the orientation change (device rotation) does not trigger a window resize event, so the method to change the layout will not fire automatically.
What is the proper way to detect the screen orientation change without listening for resizing event?
The solution to this issue is to check for a change in the aspect ratio. The condition I used:
if((double) newVal < window.getHeight() || ((double) newVal/visualBounds.getHeight() < 1.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With