How do I Create a Circular Progress bar in Expo React Native with a text in center?
Image by Rockland - hkhazo.biz.id

How do I Create a Circular Progress bar in Expo React Native with a text in center?

Posted on

Hey there, fellow developer! Are you tired of using boring, linear progress bars in your Expo React Native app? Do you want to add some visual flair to your loading screens with a sleek, circular progress bar? Well, you’re in luck because today we’re going to dive into the world of circular progress bars and learn how to create one with a text in the center using Expo React Native!

Why Circular Progress Bars?

Circular progress bars are not only aesthetically pleasing, but they’re also super effective in conveying progress to your users. By using a circular shape, you can create a sense of continuity and closure, making the loading experience feel more satisfying and engaging. Plus, they’re just plain cool!

What We’ll Cover

In this article, we’ll cover the following topics:

  • The basics of Expo React Native and its advantages
  • Understanding the concept of circular progress bars and their components
  • Creating a circular progress bar with a text in the center using React Native’s built-in components
  • Customizing the appearance and behavior of our circular progress bar
  • Handling state changes and updating the progress bar dynamically

Getting Started with Expo React Native

Before we dive into creating our circular progress bar, let’s quickly cover the basics of Expo React Native and why it’s an excellent choice for building cross-platform apps.

Expo is a framework built on top of React Native that provides a set of tools, services, and APIs to help you build, deploy, and manage your React Native apps. With Expo, you can create apps for both iOS and Android using a single codebase, and it’s an excellent choice for beginners and experienced developers alike.

expo init myapp
cd myapp
npm start

If you’re new to Expo, create a new project by running the above commands in your terminal. This will create a basic Expo project structure, and you can start building your app right away!

Understanding Circular Progress Bars

A circular progress bar typically consists of the following components:

  • A circular path that represents the progress arc
  • A fill color that indicates the progress made
  • A stroke width and color that defines the thickness and color of the progress arc
  • A text element that displays the progress percentage or a custom message

Creating the Circular Progress Bar

Now that we have a basic understanding of circular progress bars, let’s create one using React Native’s built-in components!

First, create a new file called `CircularProgressBar.js` and add the following code:

import React, { useState } from 'expo';
import { View, Text, Animated } from 'react-native';

const CircularProgressBar = () => {
  const [progress, setProgress] = useState(0);

  return (
    <View>
      <View>
        <Animated.View
          style={{
            transform: [
              {
                rotate: `${progress}deg`,
              },
            ],
            width: 100,
            height: 100,
            borderRadius: 50,
            borderColor: 'rgba(0, 0, 0, 0.2)',
            borderWidth: 2,
            backgroundColor: 'rgba(0, 0, 0, 0.1)',
          }}
        />
      </View>
      <Text
        style={{
          fontSize: 24,
          fontWeight: 'bold',
          textAlign: 'center',
          color: 'black',
        }}
      >
        {Math.floor(progress)}%
      </Text>
    </View>
  );
};

export default CircularProgressBar;

In this example, we’re using the `Animated.View` component to create the circular progress arc. We’ve also added a `Text` component to display the progress percentage in the center of the circle.

Customizing the Appearance

Now that we have a basic circular progress bar, let’s customize its appearance and behavior!

import React, { useState } from 'expo';
import { View, Text, Animated } from 'react-native';

const CircularProgressBar = ({ size, strokeWidth, progressColor, textColor }) => {
  const [progress, setProgress] = useState(0);

  return (
    <View>
      <View>
        <Animated.View
          style={{
            transform: [
              {
                rotate: `${progress}deg`,
              },
            ],
            width: size,
            height: size,
            borderRadius: size / 2,
            borderColor: 'rgba(0, 0, 0, 0.2)',
            borderWidth: strokeWidth,
            backgroundColor: 'rgba(0, 0, 0, 0.1)',
          }}
        />
      </View>
      <Text
        style={{
          fontSize: size / 4,
          fontWeight: 'bold',
          textAlign: 'center',
          color: textColor,
        }}
      >
        {Math.floor(progress)}%
      </Text>
    </View>
  );
};

export default CircularProgressBar;

In this updated version, we’ve added four props to customize the appearance of our circular progress bar:

  • `size`: The diameter of the circular progress bar
  • `strokeWidth`: The width of the progress arc
  • `progressColor`: The fill color of the progress arc
  • `textColor`: The color of the text element

Handling State Changes

Now that we have a fully customizable circular progress bar, let’s learn how to update its state dynamically!

import React, { useState } from 'expo';
import { View, Text, Animated } from 'react-native';

const CircularProgressBar = ({ size, strokeWidth, progressColor, textColor }) => {
  const [progress, setProgress] = useState(0);

  const updateProgress = (newProgress) => {
    setProgress(newProgress);
  };

  return (
    <View>
      <View>
        <Animated.View
          style={{
            transform: [
              {
                rotate: `${progress}deg`,
              },
            ],
            width: size,
            height: size,
            borderRadius: size / 2,
            borderColor: 'rgba(0, 0, 0, 0.2)',
            borderWidth: strokeWidth,
            backgroundColor: 'rgba(0, 0, 0, 0.1)',
          }}
        />
      </View>
      <Text
        style={{
          fontSize: size / 4,
          fontWeight: 'bold',
          textAlign: 'center',
          color: textColor,
        }}
      >
        {Math.floor(progress)}%
      </Text>
    </View>
  );
};

export default CircularProgressBar;

In this example, we’ve added a `updateProgress` function that updates the `progress` state variable. You can call this function from your app’s logic to update the progress bar dynamically!

Conclusion

And that’s it! You now have a fully functional circular progress bar with a text in the center using Expo React Native. Remember to customize the appearance and behavior to fit your app’s unique style and needs.

If you’re feeling adventurous, try adding more features to your circular progress bar, such as:

  • Animating the progress updates
  • Adding a secondary progress arc for multiple tasks
  • Using gradients or shadows to enhance the visual appeal

The possibilities are endless, and with Expo React Native, you have the power to create stunning, engaging apps that delight your users!

Property Description
size The diameter of the circular progress bar
strokeWidth The width of the progress arc
progressColor The fill color of the progress arc
textColor The color of the text element

Happy coding, and don’t forget to share your creations with the Expo community!

Additional Resources

For more information on Expo React Native, be sure to check out the official documentation and tutorials: