React Native indicator

React native activity indicator :

React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader.

Look at the picture below -

react native indicator

So now you must have understood what we are talking about. There are many types of activity indicators, and we will be discussing some of them.

Let's create one simple activity indicator, and then we will understand the code part by part for more clarity.

App.js code:

import React, { Component } from 'react'  
import {  
    ActivityIndicator,    
    StyleSheet,  
    Text,  
    View,  
} from 'react-native'  
 
export default class ActivityIndicatorDemo extends Component {  
    state = { animating: true }  
    closeActivityIndicator = () => setTimeout(() => this.setState({  
        animating: false }), 6000)  
    componentDidMount = () => this.closeActivityIndicator()  
    render() {  
        const animating = this.state.animating  
        return (  
            <View style={[styles.container]} >  
                <ActivityIndicator  animating = {animating} size="large" color="#990000" />  
             
            </View>  
        )  
    }  
}  
 
const styles = StyleSheet.create({  
    container: {  
        backgroundColor: "#ffb3b3",
        flex: 1,  
        justifyContent: 'center'  
    },  
    hz: {  
        flexDirection: 'row',  
        justifyContent: 'space-around',  
        padding: 20  
    }  
})

Output :

react native indicator

Explanation of code :

import {  
    ActivityIndicator,    
    StyleSheet,  
    Text,  
    View,  
} from 'react-native'

In this part, we have imported activity indicators along with other components.

If you want to import only the activity indicator, then the code will be -

import {ActivityIndicator}  from 'react-native'

We will be discussing animation more deeply in the animation part of this tutorial.

closeActivityIndicator = () => setTimeout(() => this.setState({  
        animating: false }), 6000)

 The closing of the latter is done using a close activity indicator after a specific time. It's one of the necessities. Otherwise, it will be running without stopping. Here 6000 means that it will stop after 6 seconds. You can give time as per your requirement.

 <ActivityIndicator  animating = {animating} size="large" color="#990000" /> 

In this part, we have declared the activity indicator component. Here though {animating} we are enabling the animation, as we have given true value to it.

 At last, we have declared the stylesheet and defined our desirable styling.

What if we give false to animation?

Let's see the output -

[note - here we will only be changing this part-> state = { animating: false }

The rest of the code is the same ]

react native indicator

There is no activity indicator if we declare animation as false.

This was the simplest example of an activity indicator. Let's see some different activity indicators.

In the example below, we will use both activity indicator and grid.

App.js code :

import React, { Component } from 'react'  
import {  
    ActivityIndicator,    
    StyleSheet,  
    Text,  
    View,  
} from 'react-native'  
import { Col, Row, Grid } from "react-native-easy-grid";


 
export default class ActivityIndicatorDemo extends Component {  
    state = { animating: true }  
    closeActivityIndicator = () => setTimeout(() => this.setState({  
        animating: false }), 8000)  
    componentDidMount = () => this.closeActivityIndicator()  
    render() {  
        const animating = this.state.animating  
        return (  
            <View style={[styles.container, styles.horizontal]} >  
            <Grid style={{backgroundColor:'pink',borderColor :'black'}}>
    <Col >
    <Row style={{backgroundColor:'#ecc6d9',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color="#990000" />   </Row>
      <Row style={{backgroundColor:'#ffff99',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color="#99ff66" /> </Row>
      <Row style={{backgroundColor:' #b3ffff',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color="#ff66b3" />  </Row>
      <Row style={{backgroundColor:'#fa32ed',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color=" #944dff" /> </Row>
      <Row style={{backgroundColor:'#ccffcc',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color="#ff4d4d" /> </Row>
    </Col>
    <Col  >
    <Row style={{backgroundColor:'#ccfff5',justifyContent:"center"}}><ActivityIndicator  animating = {animating} size="large" color="#ffff00" /> </Row>
      <Row style={{backgroundColor:'#d11aff',justifyContent:"center"}}> <ActivityIndicator  animating = {animating} size="large" color="#1affff" /> </Row>
      <Row style={{backgroundColor:'#f3ccff',justifyContent:"center"}}>  <ActivityIndicator  animating = {animating} size="large" color="#ffff00" /> </Row>
      <Row style={{backgroundColor:' #ffcc99',justifyContent:"center"}}><ActivityIndicator  animating = {animating} size="large" color="#800080" /></Row>
      <Row style={{backgroundColor:'#b3e0ff',justifyContent:"center"}}><ActivityIndicator  animating = {animating} size="large" color="#800080" /></Row>
     
       
    </Col>
</Grid>
  </View>
);
                  
    }  
}  
 
const styles = StyleSheet.create({  
    container: {  
        backgroundColor: "black",
        flex: 1,  
        justifyContent: 'center'  
    },  
    horizontal: {  
        flexDirection: 'row',  
        justifyContent: 'space-around',  
        padding: 20  
    }  
})

Output :

react native indicator

Explanation: Here, we have used the activity indicator and grid together. We have discussed this same grid in one example. The only difference is that there was a number inside the grid, and here we are using an activity indicator inside every row. If you are finding any difficulty in the grid, kindly check the react-native grid tutorial. From there, you can understand this concept very clearly.

Different props of react-native activity indicator :

Animation: Its name is enough to make us understand what it does. It takes a boolean value that is true or false. If it is true, then the indicator will be visible, else indicator will not be visible.

Color: It defines the color of the indicator.

Hidewhenstopped: If the indicator is not animating so, whether it should be hidden, Hidewhenstopped decides it.

Size: It defines the size of the indicator.


Related Topics

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.

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 stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method. As you know, mobile apps are...

4 minutes read.

React Native Toast

Introduction: It is a message that will be displayed on the screen for a short span of time to interrupt the user. Primarily it is displayed by any user action....

5 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 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.

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 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.

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 Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 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.

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.

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.

Lifecycle of React Native Components

We are familiar with the word “lifecycle”. In human life, the lifecycle has three phases - birthlifedeath In react native its component lifecycle is almost the same as the human lifecycle. The...

6 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 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.

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 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.

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.

Advantages and Disadvantages of React Native

Advantages of using React Native It is observed that React Native stands out from most of the existing methods of cross-platform application development, like Ionic or Cordova, because React Native renders...

3 minutes read.