7

After much research I have discovered that the Dimensions library for Android does not work like it is supposed to. For example, Each time I use "Dimensions.get('window').height", I get a different results.

This is documented on git:
Dimensions.get('window').height is sometimes wrong on Android

The solution is to use "getRealDimensions" library as is mentioned on the git link above.

This is the link to the 'getRealDimensions' but it is written in java and I have no idea how to implement it into my react-native android project.

Summary
How can I implement the 'getRealDimensions' library into my react-native android project?

Thanks.

Larney
  • 1,674
  • 4
  • 23
  • 47
  • This looks promising: https://github.com/jaysoo/react-native-extra-dimensions-android – Tushar Khatiwada Jul 26 '16 at 14:20
  • It does. I'll have a look and get back to you. Thanks – Larney Jul 26 '16 at 14:28
  • @Tushar Khatiwada I have been trying to get it working for a while now but no luck. If your in a position to help would you mind having a look at this [issue I created on Git.](https://github.com/jaysoo/react-native-extra-dimensions-android/issues/13) Thanks. – Larney Jul 26 '16 at 15:43

3 Answers3

8

For those still struggling with this problem, here is a small insight I can give.

First the API react-native (0.54) seems to be stable for Android, the call to Dimensions.get('window') returns the same value every time (at least from what I saw). On Android though, the height returned by this call is the screen with the Status Bar (i.e. the bar at the top of the screen with the wifi icon and notifications).

Second if you want to get only the height of the screen without the soft menu bar just call StatusBar.currentHeight and substract this value from the height given by Dimensions.get('window').height.

This method is fairly easy and avoid the dependance to another package (even though react-native-extra-dimensions-android is really good).

Finally if you need the total size of the screen have a look at Dimensions.get('screen'), see What's the difference between 'window' and 'screen' in the Dimensions API

Cheers

poolet
  • 371
  • 4
  • 8
2

For anyone having the same problem, here is the solution. The real solution is the react-native-extra-dimensions-android library.

The project was previously maintained by GitHub user Jaysoo, but is now maintained by Github user Sunhat

Simply install

npm install react-native-extra-dimensions-android --save

You may need to recompile the project

The library is now working!

Sunhat
  • 137
  • 1
  • 12
Larney
  • 1,674
  • 4
  • 23
  • 47
  • This is working for me. Just pay attention that you have to register the package in MainApplication.java instead of MainActivity.java – igorrmotta Sep 05 '16 at 03:58
  • After many tests, it actually doesn't work. It return the same values as Dimensions of RN 0.32 – igorrmotta Sep 05 '16 at 23:08
  • The ExtraDimension lib not working for me. Giving wrong dimension datas. I used the Dimension lib like that and giving the correct width and height: `const window = Dimensions.get('window'); const width = window.width * window.scale; const height = window.height * window.scale;` – Gergő Kajtár Mar 08 '17 at 09:29
  • If this package doesn't work for, you can *open issue tickets* on the *ORIGINAL* package, not some random fork – Sunhat Feb 25 '19 at 10:56
  • @Sunhat I see your (?) edit as an anonymous user got rejected - try making it again as yourself while logged in, maybe with some information regarding how the issue is fixed upstream, and it might get approved next time. – Adam Millerchip Feb 26 '19 at 06:31
0

Had the same issue, decided to work around it by updating the Redux store with correct dimensions from within the view onlayout callback, which seems to be giving the right width and height every time, and let redux re-render the views if needed. Haven't seen this issue in the latest react native 0.49.0 though.

nimgrg
  • 582
  • 1
  • 7
  • 17