-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dimensions.get('window').height returns wrong height on Android with notch #23693
Comments
Can you provide more information? What value did the API return vs what was expected, for example. |
@hramos Expect Dimensions.get('window').height) === 705 on Xiaomi Mi A2 lite to get the same Layout as on Google Pixel 3. |
I believe this is related to issues with |
Hi I'm seeing the same issue.
So I took a screenshot and measured out the pixels. I measured the soft menu bar to be 48 logical pixels. So I expect |
Same issue on Mi 8 Lite. |
Issue is on the new Samsung s10 model too. |
My workaround was to calculate root view height with onLayout. |
same on redmi note 7 |
I fix this like this: static height = Platform.OS === 'android' ? Dimensions.get('screen').height - StatusBar.currentHeight : Dimensions.get('window').height; |
True on Samsung Galaxy s10 model. |
I trying now fix (temporarily)like this: static height = Platform.OS === 'android' && Platform.Version > 26 ? Dimensions.get('screen').height - StatusBar.currentHeight : Dimensions.get('window').height; |
Waiting for facebook/react-native#23693 to fix devices with notch
Same problem with react-native 0.59.9, react 16.8.3 Dimensions.get('window').height returns:
React native info:React Native Environment Info: |
Based on the phones I've tested, for devices giving wrong height, Dimensions.get('screen').height is not equal to Dimensions.get('window').height.
|
@dhruvdangi exist another case with bottom navigation bar, its can be hid or showed. |
Just create something like this in app render function. There's small startup speed impact but solves the height issue if (this.state.windowHeight === 0) {
return <View onLayout={({ nativeEvent }) => {
const windowHeight = nativeEvent.layout.height;
Defaults.windowHeight = windowHeight;
this.setState({ windowHeight })
}} style={{ flex: 1 }} />
} |
You can use the following package to fix this issue: |
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
@valeriik actually, I noticed that you used I tested them and they have different values for Android: initialWindowMetrics Object {
"frame": Object {
"height": 752,
"width": 360,
"x": 0,
"y": 0,
},
"insets": Object {
"bottom": 48,
"left": 0,
"right": 0,
"top": 26.66666603088379,
},
}
frame Object {
"height": 752,
"width": 360,
"x": 0,
"y": 0,
}
insets Object {
"bottom": 0, // different here
"left": 0,
"right": 0,
"top": 26.66666603088379,
} ios: initialWindowMetrics Object {
"frame": Object {
"height": 667,
"width": 375,
"x": 0,
"y": 0,
},
"insets": Object {
"bottom": 0,
"left": 0,
"right": 0,
"top": 20,
},
}
frame Object {
"height": 667,
"width": 375,
"x": 0,
"y": 0,
}
insets Object {
"bottom": 0,
"left": 0,
"right": 0,
"top": 20,
} |
Subscribed. @valeriik and others thanks for the research |
|
Why is this closed? Further, the react-native rendering engine also has the same calculation issues. On some devices, trying to get something auto adjust its height up to 100% will end up rendered outside the screen on some devices. |
Encountered same issue. |
only solution that worked for me:
worked on: all iphones, samsung galaxy s20fe, samsung galaxy a9, redmi note 7 and motorola moto g5. |
if you do not want to install a library to get the insets of import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry'
export const getScreenMetricsForSafeArea = () => {
const { frame, insets } = TurboModuleRegistry.get('RNCSafeAreaContext').initialWindowMetrics
const { top, left, right, bottom } = insets
return { width: frame.width - (right + left), height: frame.height - (bottom + top) }
} |
An easy fix that worked for us on both Samsung S7 and Samsung A31 is the following: const height = Dimensions.get("window").height;
const isSame = height == Dimensions.get("screen").height;
const correctHeight = isSame ? height - StatusBar.currentHeight : height; |
depending on the problem this might help, take the dimensions of the screen and not the window.
|
Right now the best solution is just measure the screen yourself, and pass into a context: const Context = React.createContext();
export default function MyApp() {
const [appHeight, setAppHeight] = useState(Dimensions.get('window').height);
return (
<Context.Provider value={appHeight}>
<View style={{flex: 1}} onLayout={(e)=>setAppHeight(e.nativeEvent.layout.height)}>
<RestOfApp/>
</View>
</Context.Provider>
)
}
export function useActualHeight() {
return useContext(Context);
} |
why the hell was this closed? the issue is not fixed, and it's super important |
Still experiencing issues when trying to calculate available space |
After some testing on different android versions and different devices this what I got.
note: WINDOW_HEIGHT on emulator includes status bar height I tested this for a few versions 30, 31, 33 which is different from the real devices as mentioned above. Hope this help someone. |
I a still facing this issue |
This is still not working. Window and screen heights return almost random values and are close to useless |
@iway1-hstk immediate Nobel prize for this solution |
Resolved the issue with the following code, which seems to be working well Custom hook with a context import React, { useContext, useState } from 'react';
import { Dimensions, SafeAreaView, StyleSheet, View } from 'react-native';
const ScreenDimensionsContext = React.createContext(null);
export const useDimensions = () => {
const dimensions = useContext(ScreenDimensionsContext);
return dimensions;
};
export const ScreenDimensionsProvider = ({ children, onDimensions }) => {
const [dimensions, setDimensions] = useState(Dimensions.get('window'));
const onLayout = (layout) => {
if (onDimensions) {
onDimensions(layout);
}
setDimensions(layout);
};
return (
<ScreenDimensionsContext.Provider value={dimensions}>
<SafeAreaView style={styles.flex}>
<View
style={styles.flex}
onLayout={(e) => onLayout(e.nativeEvent.layout)}>
{children}
</View>
</SafeAreaView>
</ScreenDimensionsContext.Provider>
);
};
const styles = StyleSheet.create({
flex: {
flex: 1,
},
}); Use the context anywhere in your app and wrap your App.js in the Context const App = () => {
const [height, setHeight] = useState();
return (
<ScreenDimensionsProvider onDimensions={layout => setHeight(layout.height)}>
<View style={{height}}>
<RestOfApp />
</View>
</ScreenDimensionsProvider>
) Anywhere else in your app const RandomComponent = () => {
const { height } = useDimensions();
return <View style={{height}} />
} |
Nice implementation. I just won't have SafeAreaView there, as it might be required in some screens and not in others. |
@iway1-hstk Amazing!! |
great solution! How else can I get this value NOT through a hook... |
I don't use I hate that this works: import { Dimensions, Platform} from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
const safeAreaInsets = useSafeAreaInsets()
const safeAreaPadding = Platform.OS === 'android' ? safeAreaInsets.top + safeAreaInsets.bottom : 0
const screenHeight = Dimensions.get('window').height + safeAreaPadding ...it's nearly 2025 and a simple function to get the actual screen height still does not exist? UPDATE: Just use flexGrow for everything. |
🐛 Bug Report
Dimensions.get('window').height returns wrong height on Android with notch
Device: Android One Mi A2 Lite
Environment
React Native Environment Info:
System:
OS: macOS 10.14.3
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Memory: 1013.34 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 11.6.0 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.8.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
Android SDK:
API Levels: 18, 23, 25, 26, 27, 28
Build Tools: 19.1.0, 20.0.0, 21.1.2, 22.0.1, 23.0.1, 23.0.2, 23.0.3, 24.0.0, 24.0.1, 24.0.2, 24.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.1, 27.0.2, 27.0.3, 28.0.0, 28.0.0, 28.0.0, 28.0.1, 28.0.2, 28.0.3
System Images: android-21 | Intel x86 Atom, android-21 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom_64, android-25 | Google APIs ARM EABI v7a, android-27 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom_64
IDEs:
Android Studio: 3.3 AI-182.5107.16.33.5264788
Xcode: /undefined - /usr/bin/xcodebuild
npmPackages:
react: 16.8.3 => 16.8.3
react-native: ^0.58.5 => 0.58.5
npmGlobalPackages:
create-react-native-app: 2.0.2
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7
The text was updated successfully, but these errors were encountered: