88

How can I find the height of the status bar on Android through React Native?

If I check the React.Dimensions height the value seems to include the status bar height too but I'm trying to find the height of the layout without the status bar.

Zohar Levin
  • 2,364
  • 3
  • 19
  • 24

10 Answers10

149

You don't need any plugins for this (anymore), just use StatusBar.currentHeight:

import {StatusBar} from 'react-native';
console.log('statusBarHeight: ', StatusBar.currentHeight);

EDIT: Apparently this does seem to work on Android always, but on iOS, initially undefined is indeed returned :(

Lee Brindley
  • 6,242
  • 5
  • 41
  • 62
IjzerenHein
  • 2,690
  • 1
  • 17
  • 13
41

Unfortunately, as of v0.34, the API to do this is still inconsistent across platforms. StatusBarManager.HEIGHT will give you the current height of the Status Bar on Android.

import { Platform, NativeModules } from 'react-native';
const { StatusBarManager } = NativeModules;

const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBarManager.HEIGHT;

On iOS, you can use getHeight()

StatusBarManager.getHeight((statusBarHeight)=>{
  console.log(statusBarHeight)
})

As a side note, if you want your app to draw under the status bar on Android similar to the default iOS behavior, you can do so by setting a couple props on <StatusBar> as follows:

<StatusBar translucent={true} backgroundColor={'transparent'} {...props} />   
jmurzy
  • 3,816
  • 1
  • 17
  • 11
22

You can use this helper function which enables you to get the Status Bar height on iOS and Android. For iOS, the calculation is done to get the different StatusBar height when >= iPhone X (with notch) is used.

// functions-file.js
import { Dimensions, Platform, StatusBar } from 'react-native';

const X_WIDTH = 375;
const X_HEIGHT = 812;

const XSMAX_WIDTH = 414;
const XSMAX_HEIGHT = 896;

const { height, width } = Dimensions.get('window');

export const isIPhoneX = () => Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS
    ? width === X_WIDTH && height === X_HEIGHT || width === XSMAX_WIDTH && height === XSMAX_HEIGHT
    : false;

export const StatusBarHeight = Platform.select({
    ios: isIPhoneX() ? 44 : 20,
    android: StatusBar.currentHeight,
    default: 0
})

Then use it directly when imported:

import { StatusBarHeight } from './functions-file.js'

height: StatusBarHeight 

Another option if you use react-navigation-stack - it provides the header height via hook - https://reactnavigation.org/docs/en/stack-navigator.html#headertransparent

import { useHeaderHeight } from 'react-navigation-stack';

// ...

const headerHeight = useHeaderHeight();
dblazeski
  • 534
  • 1
  • 4
  • 7
  • This is out of data since it doesn't include newer iPhones. Looks like the package `react-native-status-bar-height` is a more up-to-date implementation of this method. – Michael Yaworski Jun 06 '22 at 05:29
8

import Constants and Dimensions.

import Constants from 'expo-constants';
import Dimensions  from 'react-native';

in stylesheet container hight - status bar hight = without status hight.

 container: {   height: Dimensions.get('window').height - Constants.statusBarHeight,}
Omar bakhsh
  • 896
  • 11
  • 18
  • 3
    And what if you're not using Expo? – 6rchid Sep 11 '21 at 05:47
  • @6rchid `expo-constants` can be installed in a bare (not expo managed) project. So @Oman answer is applicable in any case. – Gr3at Jan 01 '22 at 15:34
  • This solution is only easily useable at Expo managed workflow. This should be mentioned on top of this answer. Because the question is not asking anything directly for expo. – Kamal Hossain Jun 10 '22 at 11:19
2
import {NativeModules} from 'react-native';

// ...

const {StatusBarManager} = NativeModules;

const height = StatusBarManager.HEIGHT;
  • 2
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Feb 27 '22 at 19:49
2

If you are using @react-navigation/native-stack

import { useHeaderHeight } from '@react-navigation/elements';
const headerHeight = useHeaderHeight();

Link to source

Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
1

You can get the height of the statusbar via

  import { useSafeAreaInsets } from 'react-native-safe-area-context'

  const insets = useSafeAreaInsets()

  console.log('height of status bar', insets.top)
HannahCarney
  • 3,441
  • 2
  • 26
  • 32
0

For the situation where I need to escape a StatusBar component, my solution for Android is to use StatusBar.currentHeight to escape the StatusBar component. For iOS I use react-native's SafeAreaView component to escape the StatusBar.

Iva
  • 2,447
  • 1
  • 18
  • 28
devtucuju
  • 1
  • 4
-1

The package react-native-status-bar-height works well, however, if you don't want another dependency, I have extracted the gist here:

Create file getStatusBarHeight.tsx

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

const STATUSBAR_DEFAULT_HEIGHT = 20;
const STATUSBAR_X_HEIGHT = 44;
const STATUSBAR_IP12_HEIGHT = 47;
const STATUSBAR_IP12MAX_HEIGHT = 47;
const STATUSBAR_IP14PRO_HEIGHT = 49;

const X_WIDTH = 375;
const X_HEIGHT = 812;

const XSMAX_WIDTH = 414;
const XSMAX_HEIGHT = 896;

const IP12_WIDTH = 390;
const IP12_HEIGHT = 844;

const IP12MAX_WIDTH = 428;
const IP12MAX_HEIGHT = 926;

const IP14PRO_WIDTH = 393;
const IP14PRO_HEIGHT = 852;

const IP14PROMAX_WIDTH = 430;
const IP14PROMAX_HEIGHT = 932;

const {height: W_HEIGHT, width: W_WIDTH} = Dimensions.get('window');

let statusBarHeight = STATUSBAR_DEFAULT_HEIGHT;

if (Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS) {
  if (W_WIDTH === X_WIDTH && W_HEIGHT === X_HEIGHT) {
    statusBarHeight = STATUSBAR_X_HEIGHT;
  } else if (W_WIDTH === XSMAX_WIDTH && W_HEIGHT === XSMAX_HEIGHT) {
    statusBarHeight = STATUSBAR_X_HEIGHT;
  } else if (W_WIDTH === IP12_WIDTH && W_HEIGHT === IP12_HEIGHT) {
    statusBarHeight = STATUSBAR_IP12_HEIGHT;
  } else if (W_WIDTH === IP12MAX_WIDTH && W_HEIGHT === IP12MAX_HEIGHT) {
    statusBarHeight = STATUSBAR_IP12MAX_HEIGHT;
  } else if (W_WIDTH === IP14PROMAX_WIDTH && W_HEIGHT === IP14PROMAX_HEIGHT) {
    statusBarHeight = STATUSBAR_IP14PRO_HEIGHT;
  } else if (W_WIDTH === IP14PRO_WIDTH && W_HEIGHT === IP14PRO_HEIGHT) {
    statusBarHeight = STATUSBAR_IP14PRO_HEIGHT;
  }
}

export function getStatusBarHeight() {
  return Platform.select({
    ios: statusBarHeight,
    android: StatusBar.currentHeight,
    default: 0,
  });
}

Usage:

import {getStatusBarHeight} from './getStatusBarHeight';

export const Test = () => {

  const statusBarHeight = getStatusBarHeight();

  return <Text style={{ marginTop: statusBarHeight }}>Test</Text>

}
Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
-2
const deviceHeight = Dimensions.get('screen').height;
deviceHeight={deviceHeight}
General Grievance
  • 4,555
  • 31
  • 31
  • 45