Dimensions.get(‘window’).width incorrect in Android 6.0.1

Phone

  • Nexus 5
  • Android 6.0.1

React Native

  • 0.28.0 stable release

Problem

When running in release mode, whenever the orientation changes, the screen width/height are never updated usingDimensions.get('window'). No matter what, whether I’m in Portrait or Landscape mode, I receive this value back from the Dimensions.get call:

{ width: 360, height: 592 }

This really is only a problem because on iOS the Dimensions will get updated correctly. So I can’t just take the min of the two and call it a day.

Author: Fantashit

3 thoughts on “Dimensions.get(‘window’).width incorrect in Android 6.0.1

  1. I ran into the same issue, and came up with the same workaround, as @stoffern. We created a service that uses the react-native-orientation package to register a listener on orientation change that determines which values from the Dimensions.get('window') call are cached as the current height and width. Here’s a simplified version of what we did:

    // screenService.js
    import { Dimensions } from 'react-native';
    import Orientation from 'react-native-orientation';
    
    let screen = {};
    
    Orientation.addOrientationListener(orientation => {
      const { height, width } = Dimensions.get('window');
      const min = Math.min(height, width);
      const max = Math.max(height, width);
      const isLandscape = orientation === 'LANDSCAPE';
      screen = {
        height: isLandscape ? min : max,
        width: isLandscape ? max : min,
      };
    });
    
    export default { screen };
    

    Then we just call our module like screenService.screen.height to get the current value. The logic works for Android and iOS, so no conditional code is required. Hope that can help someone until the issue is resolved.

  2. Pingback: 사설토토

Comments are closed.

Dimensions.get(‘window’).width incorrect in Android 6.0.1

Phone

  • Nexus 5
  • Android 6.0.1

React Native

  • 0.28.0 stable release

Problem

When running in release mode, whenever the orientation changes, the screen width/height are never updated usingDimensions.get('window'). No matter what, whether I’m in Portrait or Landscape mode, I receive this value back from the Dimensions.get call:

{ width: 360, height: 592 }

This really is only a problem because on iOS the Dimensions will get updated correctly. So I can’t just take the min of the two and call it a day.

Author: Fantashit

1 thought on “Dimensions.get(‘window’).width incorrect in Android 6.0.1

  1. I ran into the same issue, and came up with the same workaround, as @stoffern. We created a service that uses the react-native-orientation package to register a listener on orientation change that determines which values from the Dimensions.get('window') call are cached as the current height and width. Here’s a simplified version of what we did:

    // screenService.js
    import { Dimensions } from 'react-native';
    import Orientation from 'react-native-orientation';
    
    let screen = {};
    
    Orientation.addOrientationListener(orientation => {
      const { height, width } = Dimensions.get('window');
      const min = Math.min(height, width);
      const max = Math.max(height, width);
      const isLandscape = orientation === 'LANDSCAPE';
      screen = {
        height: isLandscape ? min : max,
        width: isLandscape ? max : min,
      };
    });
    
    export default { screen };
    

    Then we just call our module like screenService.screen.height to get the current value. The logic works for Android and iOS, so no conditional code is required. Hope that can help someone until the issue is resolved.

Comments are closed.