React Native StatusBar
This post will help you to Add StatusBar in React Native App in Android and IOS. React Native StatusBar is a component to show the indicators like the battery, network, notification, etc. React Native by default doesn’t understand the status bar and render the view from the top left corner of the screen and override the status bar. To avoid that we need to give top margin for IOs and Android both but as you know the hight of the status bar of both the platform is different so we need a smart solution so here is the status bar which will help you to reserve the space for the StatusBar and you can start working directly.
For those who are confused in Statusbar and Actionbar, here is the difference
To Import StatusBar in Code
import { StatusBar } from 'react-native'
Render Using
<StatusBar
barStyle = "dark-content"
// dark-content, light-content and default
hidden = {false}
//To hide statusBar
backgroundColor = "#00BCD4"
//Background color of statusBar only works for Android
translucent = {false}
//allowing light, but not detailed shapes
networkActivityIndicatorVisible = {true}
/>
The react native StatusBar component only supports backgroundColor for Android only. So to change the color of the IOS status bar you have to wrap the Status Bar with a view that will have a hight of the status bar and background color.
<View
style={{
backgroundColor: '#00BCD4',
height: Platform.OS === 'ios' ? 20 : StatusBar.currentHeight,
}}>
<StatusBar
translucent
backgroundColor="#00BCD4"
barStyle="light-content"
/>
</View>
In this example, we will see how you can add Statusbar in your React Native application. So Let’s get started
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Code
Open App.js in any code editor and replace the code with the following code
App.js
In the below code we have used the backgroundColor
to change the background of the status bar but IOS does not have any concept of status bar background color so it will not reflect in IOS devices. If you still want to change the background color of the status bar in IOS you can find the next code sample below this code to set the background color of the status bar in IOS.
//React Native StatusBar
//https://aboutreact.com/react-native-statusbar/
//import React in our code
import React, {useState} from 'react';
//import all the components we are going to use
import {
SafeAreaView,
Button,
Text,
StyleSheet,
StatusBar,
View,
} from 'react-native';
const App = () => {
const styleTypes = ['default', 'dark-content', 'light-content'];
const [
visibleStatusBar,
setVisibleStatusBar
] = useState(false);
const [
styleStatusBar,
setStyleStatusBar
] = useState(styleTypes[0]);
const changeVisibilityStatusBar = () => {
setVisibleStatusBar(!visibleStatusBar);
};
const changeStyleStatusBar = () => {
const styleId = styleTypes.indexOf(styleStatusBar) + 1;
if (styleId === styleTypes.length) {
return setStyleStatusBar(styleTypes[0]);
}
return setStyleStatusBar(styleTypes[styleId]);
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<View>
<Text style={styles.textStyle}>
StatusBar Style: {styleStatusBar}
</Text>
<Text style={styles.textStyle}>
StatusBar Visibility: {
!visibleStatusBar ? 'Visible' : 'Hidden'
}
</Text>
</View>
<StatusBar
backgroundColor="blue"
barStyle={styleStatusBar}
/>
<View>
<StatusBar hidden={visibleStatusBar} />
</View>
<View style={styles.buttonContainer}>
<Button
title="Toggle StatusBar"
onPress={() => changeVisibilityStatusBar()}
/>
</View>
<View style={styles.buttonContainer}>
<Button
title="Change StatusBar Style"
onPress={() => changeStyleStatusBar()}
/>
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: 20,
backgroundColor: '#ECF0F1',
padding: 8,
},
buttonContainer: {
padding: 10,
},
textStyle: {
textAlign: 'center',
},
});
export default App;
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshot
IOS
Android
Output in Online Emulator
That was the React Native StatusBar. If you have any doubts or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!
Hope you liked it. 🙂
what if we use safeArea instead.both will serve the same purpose.isn’t it?
Kind of but at the same time they are different components too. With SafeArea you can leave the default StatusBar as it is and with StatusBar you can customise the default one.