react-native: `StatusBar.currentHeight` gives incorrect height on Google Pixel 5a (Android 12)
Description
StatusBar.currentHeight
provides an incorrect value for the height of the status bar on a Google Pixel 5a with Android 12. From basic testing, this does not seem to be an issue on other devices or on an older OS (Android 11)
Screenshot when using StatusBar.currentHeight
as the top margin
Expected:
Version
0.66.0
Output of npx react-native info
example on Expo, this is occurring in multiple versions of react native, notably tested on 0.66.0
and 0.64.2
Steps to reproduce
- use
StatusBar.currentHeight
- compare calculated height on Google Pixel 5a (Android 12) to other Android devices - the height for Pixel 5a Android 12 does not match the full height as it appears on the device while other devices do still work
Snack, code example, screenshot, or link to a repository
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 10
- Comments: 23 (3 by maintainers)
Stop auto closing these! This is a bug! And why doesn’t the RN team respond to something like this?
Not just Pixel 5a, but many devices that may have a notch.
Hi ! We quick fixed it statically until we have time to tackle it properly or it gets fixed. It look like this:
The
initialWindowMetrics?.insets.top
actually returns the correct insets. (Obviously it would be better to use auseInsets
and the context to be sure to have it updated in case it change dynamically)Let me know if it’s unclear !
We ended up using react-native-safe-area-context which were luckily already using (I hate adding lib to palliate a bug in a native function)…
It gives the insets of the devices, with the correct value for the top inset (the status bar). They probably don’t rely on
StatusBar.currentHeight
, but on the native function instead, which makes it perfectly reliable.It also enforces a better usage of this value which can actually change when rotating the screen. So we shouldn’t get it statically, but react to its changes instead. And react-native-safe-area-context provides the tools for it.
Again, not the best solution as it requires to add a lib, but at least the lib provides the right values !
Hope it can help until it’s fix on React-Native side !
` Working solution for API 29+ It was with this version that Google introduced edge-to-edge
public void getStatusBarHeight(Promise promise) {
Well finally I managed to get the exact value, using some deprecated functions. What I did was detect the full screen height (2400) and supress the innerheight (2222) from it and the navbar too (42) so I ended with the long awaited 136.
EDIT: Turns out this method only worked on Pixels, so the final result is a mix of those 2: