29

I'm trying to get the Android Bottom bar working (the bar where you have the back button) height with RN. I did the following:

Dimensions.get('window').height

I get the height with this bar! Since it can or cannot be there and can be bigger or larger depending on the settings, this is a big issue for me.

phrogg
  • 888
  • 1
  • 13
  • 28
Skïp
  • 401
  • 1
  • 4
  • 10

8 Answers8

47
  • In iOS devices, screenHeight === windowHeight;
  • In Android devices with bottom navigator bar, screen height === windowHeight + statusBarHeight + bottomNavigatorBarHeight ;
  • In Android devices without bottom navigator bar, bottomNavigatorBarHeight is zero.
import {Dimensions, StatusBar} from 'react-native'; 

const SCREEN_HEIGHT = Dimensions.get('screen').height; // device height
const STATUS_BAR_HEIGHT = StatusBar.currentHeight || 24; 
const WINDOW_HEIGHT = Dimensions.get('window').height;

enter image description here

RY_ Zheng
  • 3,041
  • 29
  • 36
  • 3
    I've found that it seems to vary a little on Android. Newer versions of Android this seems correct, older versions `windowHeight` seemed to be including the `statusBarHeight` too - which is quite confusing. – Ian May 26 '20 at 15:08
  • Hi, should you mind sharing more details? Which version and device model do you use? – RY_ Zheng May 28 '20 at 08:41
  • Using a basic formula to set a minimum CSS height `Dimensions.get('window').height - HEADER_HEIGHT - FOOTER_HEIGHT` (where HEADER_HEIGHT = 400, FOOTER_HEIGHT = 200) this is 100% of the screen height on a newer device (e.g. Nokia 8.1 and a OnePlus6T phone). On all the emulators I'm using and a Galaxy S6 I need to subtract the `statusBarHeight` as well for it to be exactly 100% – Ian May 29 '20 at 19:56
  • FYI, `statusBarHeight` does not exist on react-native's `StatusBar` anymore, you need to use `StatusBar.currentHeight`. – Shane Sepac Jul 26 '21 at 17:08
10

I use sheet below to do that:

// RN version:0.57.0
let deviceH = Dimensions.get('screen').height;
// the value returned does not include the bottom navigation bar, I am not sure why yours does.
let windowH = Dimensions.get('window').height;
let bottomNavBarH = deviceH - windowH;
wkm
  • 347
  • 3
  • 6
  • 1
    I think the problem with this method is it will also account for the status bar height. For example i have gesture navigation turned on so no navbar to display but it is still returning a height of 25 for the status bar. – Aidan Doherty Nov 13 '19 at 15:21
9
import { Dimensions , StatusBar } from 'react-native';

const screenHeight = Dimensions.get('screen').height;
const windowHeight = Dimensions.get('window').height;
const navbarHeight = screenHeight - windowHeight + StatusBar.currentHeight;
Vampire
  • 109
  • 1
  • 4
3

There is a new library which works like a charm

https://github.com/ConnectyCube/react-native-android-navbar-height

import { Dimensions } from "react-native";
import { getNavigationBarHeight } from "react-native-android-navbar-height";

// ...

const scale = Dimensions.get('screen').scale;
const navigationBarHeight = await getNavigationBarHeight();
const result = navigationBarHeight / scale;
Rubycon
  • 18,156
  • 10
  • 49
  • 70
1

first we need import

import { Dimensions , StatusBar } from 'react-native';

WindowHeight = Dimensions.get('window').height . ScreenHeight = Dimensions.get('screen').height . StatusBarHeight = StatusBar.currentHeight

WindowHeight = ScreenHeight - NavigationBarHeight

when we want to implement float button in the bottom of our page set WindowHeight for that's height and set bottom : StatusBar.currentHeight for child element .

when we don't have NavigationBar , WindowHeight = ScreenHeight

Mojtaba Darzi
  • 328
  • 5
  • 10
1

First as mentioned in other answers using react native Dimensions API in Android the window height= screen height - (status bar height + navigation bar height)

After few hours of investigations I found that in some Android devices Dimensions API gives a wrong window height number.

the solution for is using [react-native-extra-dimensions-android][1]

[1]: https://github.com/Sunhat/react-native-extra-dimensions-android it give you the following information:

REAL_WINDOW_HEIGHT - Actual height of screen including system decor elements REAL_WINDOW_WIDTH - Actual width of screen including system decor elements STATUS_BAR_HEIGHT - Height of the status bar SOFT_MENU_BAR_HEIGHT - Height of the soft menu bar (supported on most new Android devices)

then you can calculate window height: REAL_WINDOW_HEIGHT -(STATUS_BAR_HEIGHT + SOFT_MENU_BAR_HEIGHT)

basel juma
  • 1,987
  • 1
  • 10
  • 5
0
const navbarHeight = screenHeight - windowHeight + StatusBar.currentHeight;

In the landscape case, this is wrong and will get a negative number.

Soul
  • 1
-3

If you're referring to the status bar on top.

According to react native docs you can use

import { StatusBar } from 'react-native';

then in your code

StatusBar.currentHeight

UPDATE

You can get this using this module react-native-extra-dimensions-android

To detect if the soft android nav is present, you can use this module react-native-detect-navbar-android.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Mohamed Khalil
  • 3,036
  • 1
  • 20
  • 26
  • 6
    The OP is talking about the on-screen navigation bar with the back and home buttons not the status bar. – Dan Philip Bejoy Sep 09 '17 at 05:24
  • Thanks ! Is there a way to know if it il present or not ? – Skïp Sep 09 '17 at 12:53
  • yes you can use this [react-native-detect-navbar-android](https://www.npmjs.com/package/react-native-detect-navbar-android) – Mohamed Khalil Sep 09 '17 at 13:03
  • It seems to do the trick. I didn't have the time to fully check it out yet, especially when there is no bar present. For now it will do ! Thanks a lot ! – Skïp Sep 19 '17 at 13:31