React Native Status Bar

What is the status bar?

The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's wifi/data connection or speed, battery percentage, and network. This part is known as a status bar. i think now it is crystal clear.

In this part of the tutorial, you will be learning about the react-native status bar. The motive for learning about the status bar is to know how to control its appearance of the status bar. Learning and using the status bar is easy. Its working totally depends upon its property, so if you know properties well, then using it will be simple. It consists of very useful features. one interesting fact is that react-native does not know the status bar by default. Mounting more than one react native status bar is possible. Let's learn about the attributes of react-native then we will implement the react-native status bar.

Attributes/props of the react-native status bar :

Animated: It is mainly for the transition of the status bar property, and it should be animated. It supports the props such as backgroundColor, bar style, and hidden. It accepts boolean values, which can either be true or false.

backgroundColor: This is one of the props that we are repeatedly using, and you must have understood what it would be doing here. It is responsible for the background color of the status bar.

barStyle : It is used for font designing. If you want to change a font like fontSize , fontColor, fontWeight, etc., in that case, you can use barstyle to design or alter the style of the Font.

Hidden: If we want to add a hide or show feature to our react native status bar, we can use Hidden. If you do not declare or use it, it will take "false" by default. It accepts a boolean value. If it's true, then the status bar will not be visible. Else it will be visible.

networkActivityIndicatorVisible : It accepts boolean values, and this is mainly for IOS. It indicates network activity.

showHideTransition: If you want to make the show's transition and hide attractive or impressive, this can be used.

Translucent: If you want your status bar to get semi-transparent color, this prop will be useful.

Let's try to understand it by implementing it practically 

App.js code :

import * as React from 'react';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Text, Button, StatusBar,StyleSheet } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { NavigationContainer } from '@react-navigation/native';
import SafeAreaView from 'react-native-safe-area-view';




function dark_Screen({ navigation }) {
  return (
    <SafeAreaView style={[styles.container, { backgroundColor: 'black' }]}>
      <StatusBar barStyle="light-content" backgroundColor="#6a51ae" />
      <Button
        title="Next screen"
        onPress={() => navigation.navigate('Screen2')}
        color="red"
      />
       <Text style={{ color: '#fff',fontSize:25,fontWeight:'bold' }}>This is dark Screen :)</Text>
    </SafeAreaView>
  );
}




function light_Screen({ navigation }) {
  return (
    <SafeAreaView style={[styles.container, { backgroundColor: '#ecf0f1' }]}>
      <StatusBar barStyle="dark-content" backgroundColor="#ecf0f1" />
      <Text style={{fontWeight:"bold",fontSize:25}}>This is light Screen :)</Text>
      <Button
        title="Next screen"
        onPress={() => navigation.navigate('Screen1')}
      />
    </SafeAreaView>
  );
}




const Stack = createNativeStackNavigator();




export default function App() {
  return (
    <SafeAreaProvider>
      <NavigationContainer>
        <Stack.Navigator screenOptions={{ headerShown: false }}>
          <Stack.Screen name="Screen1" component={dark_Screen} />
          <Stack.Screen name="Screen2" component={light_Screen} />
        </Stack.Navigator>
      </NavigationContainer>
    </SafeAreaProvider>
  );
}




const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
});

Output:

React native Alert
React native Alert

Click on the next screen you can find that you are switching between a light screen and a dark screen. Let's learn how it's actually happening.

Explanation :

Firstly we have imported all the necessary components and then made functions for respective pages. Lets understand the code below-

Dark screen:

function dark_Screen({ navigation }) {
  return (
    <SafeAreaView style={[styles.container, { backgroundColor: 'black' }]}>
      <StatusBar barStyle="light-content" backgroundColor="#6a51ae" />
      <Button
        title="Next screen"
        onPress={() => navigation.navigate('')}
        color="red"
      />
       <Text style={{ color: '#fff',fontSize:25,fontWeight:'bold' }}>This is dark Screen :)</Text>
    </SafeAreaView>
  );

Firstly, we have declared a function and given its name dark_screen. Then, we have wrapped our whole content in safeAreaView to ensure that things are properly aligned. We have used the status bar and styled it .button is used to switch between two pages. If we tap the button, we will be directed to the light page.

Same things are done to light_page content.The only difference is in the background color.

The interesting thing is how we have connected two pages ?

Here stack navigator plays major role , we will be learning about it in brief after some parts.Here is the code-

          <Stack.Screen name="Screen1" component={dark_Screen} />

          <Stack.Screen name="Screen2" component={light_Screen} />

So this is how the whole code is functioning.

Methods of the react-native status bar :

popStackEntry: If you want to remove the last statusbar from the stack, you can use this method.

pushStackEntry: It can be used to put a status bar entry on the available stack. After completion value must be returned to popStackEntry.

replaceStackEntry: This can be used if you want to replace your stacks prop. It updates the stack entry with the new property.

setBackgroundColor: This is used to set the status bar's background color.

setBarStyle : It is used for styling the react-native status bar.

 set hidden: This method is used for hiding and showing features of the status bar.

setNetworkActivityIndicatorVisible : The visibility of the network activity indicator can be controlled by it.

setTranslucent : This method is used to control translucent prop.


Related Topics

HTTP in React Native

Networking HTTP in React-native: we all are aware that react native is a javascript framework.HTTP is nothing but an API request, and you might think that what is the need for an...

4 minutes read.

React Native Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.

React Native Fonts

The font is one of the important essences of any good UI(User Interface). It is very important to have a clear and beautiful font in your app. In this module,...

4 minutes read.

React Native Animations

Animation: animation is a method in which puppets, figures, drawings, etc., appear as moving creatures, making an illusion that things are moving. It is a unique way of expressing something...

6 minutes read.

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

Redux in React Native

 It is a really amazing library of javascript, react, and react-native. It is very popular in react native domain. Every react and react native developer must know the redux it...

7 minutes read.

TouchableHighlight in React Native

It is just a wrapper for making touch work properly, and it is the conventional way of handling touch gestures. Today's most future-proof solution is pressable API. pressable manages all...

2 minutes read.

React Native Pressable

It manages all the touch inputs, and it is one of the core wrappers introduced in July 2022. The Two important features of pressable are listed below- onPressInonPressOut onpressIn: We already know the...

3 minutes read.

Basic knowledge of firebase for React Native

Firebase for react-native : Firebase is a mobile and web application development platform which is created in 2014 by google. It is a product of google which is offered to developers...

7 minutes read.

Why to Learn React Native

The demand for smartphone applications is ever-changing. The elder ones disappear from our remembrances, and smartphones and new applications come each day. In this turbulent ecosystem, it is struggling for...

4 minutes read.

React Native API

API API stands for an Application programming interface. API allows communication between two software and is also responsible for data sharing. It takes the asked data from the server and delivers...

6 minutes read.

Difference Between React.js and React Native

React js vs. React-native React native:  react-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. Facebook released it in 2015 and witnessed...

3 minutes read.

React Native navigation bar and button

Here we will be focusing on header navigation. We will add buttons to the header, which is the best way to interact with a header. We have already learned about...

4 minutes read.

React native calender

In this article, we will be learning to add calendar features to react native app. So before creating the calendar, we do need to install a few packages or add some...

3 minutes read.

React Native Components

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

16 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.

TouchableOpacity in React Native

Touchable opacity is the most suited example if you want to make views respond to proper touch. If you want to control the opacity then it can be done by...

3 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including...

5 minutes read.