Implemented own useWindowDimensions hook for Android.
π‘ Motivation and Context
The problem with default implementation of useWindowDimensions on Android is that it doesn't work well with edge-to-edge mode and you can not retrieve an actual size of the screen. Here is a brief comparison of values captured on my device (Pixel 7 Pro).
So as you can see it doesn't react properly on the case when StatusBar is translucent and reports incorrect values, which later on causes incorrect layout calculation in components like KeyboardAvoidingView or KeyboardAwareScrollView.
Theoretically we could workaround this problem by original useWindowDimensions().height + StatusBar.currentHeight, but everything become trickier when we add translucent navigationBar (+ translucent statusBar):
In this case derived value useWindowDimensions().height + StatusBar.currentHeight (867.4285888671875) still will produce incorrect value and all calculations will be broken. So I decided to create own version of the hook which will cover all the cases.
π Description
Implemented own
useWindowDimensions
hook for Android.π‘ Motivation and Context
The problem with default implementation of
useWindowDimensions
on Android is that it doesn't work well withedge-to-edge
mode and you can not retrieve an actual size of the screen. Here is a brief comparison of values captured on my device (Pixel 7 Pro).Translucent
StatusBar
:Non translucent
StatusBar
:So as you can see it doesn't react properly on the case when
StatusBar
is translucent and reports incorrect values, which later on causes incorrect layout calculation in components likeKeyboardAvoidingView
orKeyboardAwareScrollView
.Theoretically we could workaround this problem by original
useWindowDimensions().height + StatusBar.currentHeight
, but everything become trickier when we add translucentnavigationBar
(+ translucentstatusBar
):In this case derived value
useWindowDimensions().height + StatusBar.currentHeight
(867.4285888671875) still will produce incorrect value and all calculations will be broken. So I decided to create own version of the hook which will cover all the cases.Issue for reference: https://github.com/facebook/react-native/issues/41918
Closes https://github.com/kirillzyusko/react-native-keyboard-controller/issues/434 https://github.com/kirillzyusko/react-native-keyboard-controller/issues/334
π’ Changelog
JS
useWindowDimensions
hook;useWindowDimensions
inKeyboardAwareScrollView
andKeyboardAvoidingView
components;useWindowDimensions
hook as default RN implementation;Android
WindowDimensionsListener
class;ThemedReactContext.content
extension;ThemedReactContext.setupWindowDimensionListener
extension;π€ How Has This Been Tested?
Tested manually on Pixel 7 Pro (API 34).
Tested on CI via e2e (API 28).
πΈ Screenshots (if appropriate):
Pixel 7 Pro (Android 14),
KeyboardAwareScrollView
:KeyboardAwareScrollView
KeyboardAvoidingView
π Checklist