Hello friends, As you all know from now on we are trying our best to cover one of the most popular react native package React Native Paper. We have post a tutorial regarding it’s installation earlier. Now in today’s tutorial we would learn about ActivityIndicator component of react native paper. We would make a simple example with all of their props and show and hide the ActivityIndicator on button click. The button we would use is also from react native paper. So in this tutorial we would learn about Example of React Native Paper ActivityIndicator Chapter – 2.
Contents in this project Example of React Native Paper ActivityIndicator Chapter – 2 :-
1. If you are new to react native paper, Then before going further you have to install the React Native Paper package in your react native project.
2. Open your project’s main App.js file and import useState, StyleSheet, SafeAreaView from react native and ActivityIndicator, Button, Provider as PaperProvider from react-native-paper package.
1
2
3
4
5
|
import React, { useState } from ‘react’;
import { ActivityIndicator, Button, Provider as PaperProvider } from ‘react-native-paper’;
import { StyleSheet, SafeAreaView } from ‘react-native’;
|
3. Creating our Main component.
1
2
3
4
5
|
export default function Main() {
}
|
4. Creating 2 States named as show with State update method setShow and text State with State update method setText. We would use the show state to show and hide the ActivityIndicator and text State to change the Button above text.
1
2
3
|
const [show, setShow] = useState(true);
const [text, setText] = useState(‘Hide ActivityIndicator’);
|
5. Creating a function named as let_show_hide(). We would use this function to Toggle show and hide functionality.
1
2
3
4
5
6
7
8
9
10
|
const let_show_hide = () => {
if (show === true) {
setShow(false);
setText(‘Show ActivityIndicator’)
} else {
setShow(true);
setText(‘Hide ActivityIndicator’)
}
}
|
6. Creating return() block, Now here we would make 3 Sizes ActivityIndicator.
Props :-
- animating : Support Boolean value, Used to show and hide the Activity Indicator.
- color : Support all type of color format.
- size : Support 3 size formats. small, large and given number format.
- hidesWhenStopped : Hide the activity indicator when it is not animating.
- style : Used to apply custom styling. Support all the View component style prop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<ActivityIndicator
animating={show}
color={‘red’}
size={‘small’}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
<ActivityIndicator
animating={show}
color={‘green’}
size={‘large’}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
<ActivityIndicator
animating={show}
color={‘purple’}
size={40}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
|
7. Creating a Button and call the let_show_hide method on button onPress event.
1
2
3
4
5
6
|
<Button
mode=“contained”
style={styles.buttonStyle}
onPress={let_show_hide}>
{text}
</Button>
|
8. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
buttonStyle: {
width: 300,
height: 45,
alignItems: ‘center’,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
|
9. Complete Source Code for App.js file :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import React, { useState } from ‘react’;
import { ActivityIndicator, Button, Provider as PaperProvider } from ‘react-native-paper’;
import { StyleSheet, SafeAreaView } from ‘react-native’;
export default function Main() {
const [show, setShow] = useState(true);
const [text, setText] = useState(‘Hide ActivityIndicator’);
const let_show_hide = () => {
if (show === true) {
setShow(false);
setText(‘Show ActivityIndicator’)
} else {
setShow(true);
setText(‘Hide ActivityIndicator’)
}
}
return (
<PaperProvider>
<SafeAreaView style={styles.MainContainer}>
<ActivityIndicator
animating={show}
color={‘red’}
size={‘small’}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
<ActivityIndicator
animating={show}
color={‘green’}
size={‘large’}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
<ActivityIndicator
animating={show}
color={‘purple’}
size={40}
hidesWhenStopped={true}
style={{ marginBottom: 30 }} />
<Button
mode=“contained”
style={styles.buttonStyle}
onPress={let_show_hide}>
{text}
</Button>
</SafeAreaView>
</PaperProvider>
);
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
buttonStyle: {
width: 300,
height: 45,
alignItems: ‘center’,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
|
Screenshots :-