Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect screen orientation change? - JavaFX on Microsoft Surface

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?

like image 425
mhopey Avatar asked Aug 14 '17 17:08

mhopey


1 Answers

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) 
like image 71
mhopey Avatar answered Oct 28 '22 12:10

mhopey