React Native View Pager with 3 Different Indicators

React Native View Pager

This is an example of React Native View Pager with 3 Different Indicators. React Native View Pager allows the user to flip left and right through pages. In our ViewPager Example, we’ll implement a ViewPager that swipes through three different views with the indicator.

  1. PagerDotIndicator
  2. PagerTabIndicator
  3. PagerTitleIndicator

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.

Installation of Dependency

To use PagerTabIndicator, IndicatorViewPager, PagerTitleIndicator, PagerDotIndicator you need to install @shankarmorwal/rn-viewpager package. To install this

Open the terminal and jump into your project

cd Projectname

Run the following command

npm install @shankarmorwal/rn-viewpager --save

This command will copy all the dependencies into your node_module directory. –save is optional.

Code for React Native View Pager

Now Open App.js in any code editor and replace the code with the following code

App.js

// React Native View Pager with 3 Different Indicators
// https://aboutreact.com/react-native-view-pager/

// import React in our code
import React from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text
} from 'react-native';

// import {
//   PagerTabIndicator,
//   IndicatorViewPager,
//   PagerTitleIndicator,
//   PagerDotIndicator,
// } from 'rn-viewpager';

import {
  PagerTabIndicator,
  IndicatorViewPager,
  PagerTitleIndicator,
  PagerDotIndicator,
} from '@shankarmorwal/rn-viewpager';

const App = () => {
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <IndicatorViewPager
          style={styles.pagerStyle}
          indicator={
            <PagerDotIndicator pageCount={3} />
          }>
          <View style={styles.background1}>
            <Text style={styles.textStyle}>One</Text>
          </View>
          <View style={styles.background2}>
            <Text style={styles.textStyle}>Two</Text>
          </View>
          <View style={styles.background3}>
            <Text style={styles.textStyle}>Three</Text>
          </View>
        </IndicatorViewPager>

        <IndicatorViewPager
          style={styles.pagerStyle}
          indicator={
            <PagerTitleIndicator
              titles={['one', 'two', 'three']}
            />
          }>
          <View style={styles.background1}>
            <Text style={styles.textStyle}>One</Text>
          </View>
          <View style={styles.background2}>
            <Text style={styles.textStyle}>Two</Text>
          </View>
          <View style={styles.background3}>
            <Text style={styles.textStyle}>Three</Text>
          </View>
        </IndicatorViewPager>

        <IndicatorViewPager
          style={styles.pagerStyle}
          indicator={
            <PagerTabIndicator
              tabs={[
                {
                  text: 'Home',
                  // iconSource:
                  //   require('./resources/home.png'),
                  // selectedIconSource:
                  //   require('./resources/home_selected.png'),
                },
                {
                  text: 'Message',
                  // iconSource:
                  //     require('./resources/message.png'),
                  // selectedIconSource:
                  //     require('./resources/message_selected.png'),
                },
                {
                  text: 'Profile',
                  // iconSource:
                  //   require('./resources/profile.png'),
                  // selectedIconSource:
                  //    require('./resources/profile_selected.png'),
                },
              ]}
            />
          }>
          <View style={styles.background1}>
            <Text style={styles.textStyle}>Home</Text>
          </View>
          <View style={styles.background2}>
            <Text style={styles.textStyle}>Message</Text>
          </View>
          <View style={styles.background3}>
            <Text style={styles.textStyle}>Profile</Text>
          </View>
        </IndicatorViewPager>
      </View>
    </SafeAreaView>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  background1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#C70039',
  },
  background2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FF5733',
  },
  background3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFC300',
  },
  textStyle: {
    color: 'white',
    fontSize: 30,
  },
  pagerStyle: {
    flex: 1,
    paddingTop: 20,
    backgroundColor: 'white',
  },
});

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 Screenshots

That was the React Native ViewPager. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

13 thoughts on “React Native View Pager with 3 Different Indicators”

    • Hi Ravi,
      To change the color of the DotIndicator you need to override the default prop using dotStyle and selectedDotStyle where you can change the background color of the dot indicator. Please change the PagerDotIndicator with the following lines and you will see the change.

      
       _renderDotIndicator() {
          return <PagerDotIndicator pageCount={3} 
          dotStyle={{backgroundColor:"green"}}
          selectedDotStyle={{backgroundColor:"yellow"}}/>;
        }
        

      In case of any issue please comment.

      Reply
  1. Hello ~
    Thank you for your tutorial , It’s very useful with me.
    Could i make indicator in top of the view pager ?

    Reply
  2. I Have to make a home activity in which side navigation drawer and slider exist, for slider I have to make dynamic IndicatoViewPager with dynamic PagerDotIndicator,
    could you please help.

    Thanks..

    Reply
  3. Hi Brother, i’m getting this error:: Invariant Violation: requireNativeComponent: “RNCViewPager” was not found in the UIManager.

    Can you please address this problem.Thanks

    Reply
    • Can you please try installing npm install @react-native-community/viewpager --save

      Note: I haven’t tried it with Expo as I am heavily not interested in Expo, It worked for me in non-expo environment.

      Actually the real owner of the library is not maintaining the Repo so everybody is forking the Repo and I can find any perfect one. You can try it once else I’ll try to search something else.

      Reply
  4. hello sir,
    This code ipone 12 pro max not working, issue is that indicator not updates properly.
    could you please help…

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.