react-native: useWindowDimensions + translucent system ui returns incorrect dimensions on android

Description

The window height doesn’t include the navigation bar height when it’s displayed as “translucent”. This also occurs if you enable edge-to-edge mode, which similarly shows the app UI under the system UI.

Steps to reproduce

Install and run the reproducer project

React Native Version

0.73.0

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 13.6
  CPU: (8) arm64 Apple M1 Pro
  Memory: 86.08 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.17.1
    path: ~/.nvm/versions/node/v18.17.1/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.nvm/versions/node/v18.17.1/bin/yarn
  npm:
    version: 9.6.7
    path: ~/.nvm/versions/node/v18.17.1/bin/npm
  Watchman:
    version: 2023.09.04.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.11.3
    path: /Users/joeporpeglia/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.0
      - iOS 17.0
      - macOS 14.0
      - tvOS 17.0
      - watchOS 10.0
  Android SDK: Not Found
IDEs:
  Android Studio: 2022.2 AI-222.4459.24.2221.10121639
  Xcode:
    version: 15.0/15A240d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 11.0.17
    path: /Users/joeporpeglia/.sdkman/candidates/java/current/bin/javac
  Ruby:
    version: 2.7.5
    path: /Users/joeporpeglia/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.0
    wanted: 0.73.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

n/a

Reproducer

https://github.com/joeporpeglia/rn-android-window-dimensions-reproducer

Screenshots and Videos

In the screenshots below the height of the blue view is set to useWindowDimensions().height.

Navigation bar height is excluded on all android devices I tested

Status bar height is excluded on Pixel 5 device

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 4
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I was facing the same issue with useWindowDimensions returning incorrect window height on Android, especially with translucent system UI. Instead of using useWindowDimensions , I switched to useSafeAreaFrame , and it provided the correct height, accounting for the safe area and navigation bar. Hope this helps anyone facing a similar issue!

Please correct me if I’m wrong, but i believe this is the intended behavior, @joeporpeglia.

In React Native, useWindowDimensions provides the dimensions of the window (or viewport), which differ from the full screen dimensions. This difference is particularly noticeable in cases where translucent or hidden system UI elements, like the navigation bar, are present.

Internally, useWindowDimensions utilizes the DisplayMetrics object retrieved from context.getResources().getDisplayMetrics(). This method in Android does exclude the navigation bar in its calculations. It returns metrics based on the current display metrics, representing the screen area available to your application. This typically exclude system UI elements such as the navigation bar.

Currently, there isn’t a dedicated hook in React Native for directly accessing the full “Screen” dimensions. However, in situations where you need to consider the entire screen size - particularly in apps that use a translucent navigation bar or operate in edge-to-edge mode - I suggest using the following approach:

import { Dimensions } from 'react-native';

const screenWidth = Dimensions.get('screen').width;
const screenHeight = Dimensions.get('screen').height;

This method will provide you with the full dimensions of the screen, inclusive of the areas covered by system UI elements, which is crucial for accurate layout and design in such specific scenarios.


cc @cortinico As a side note, I believe adding a dedicated hook for screen dimensions could be beneficial for users. I’m considering creating such a hook in the core library, tentatively named useScreenDimensions. What do you think?

Also one aspect of this issue is that the reported window height doesn’t change on an Android device when you set the status bar translucent, while according to the documentation at https://reactnative.dev/docs/dimensions it says “For Android the window dimension will exclude the size used by the status bar (if not translucent) and bottom navigation bar”. So it should be excluded if not translucent, and included if it is translucent, which makes sense since the zero point of the drawable area changes according to the translucency.

And then another aspect of this is the fact others have mentioned that it depends on the device whether the status bar area is included in the window height or not, which makes it very hard to make calculations based on the window height since you don’t know if it includes the status bar or not.

Nothing worked for me except this solution https://github.com/facebook/react-native/issues/47080#issuecomment-2508935820

Basically make the View of your screen container absolute

<View
            style={[
              styles.container,
              {
                position: "absolute",
                right: 0,
                left: 0,
                top: 0,
                bottom: 0,
              },
            ]}
          >
... all of your contents
</View>