SnackBar is a custom bottom to top fixed sized swipeable layout containing 1 line text message with UNDO button, It would automatically hide after a given time. SnackBar is used to show light weight feedback on any operation in android mobile phone applications, But now using React Native mobile application programming language we could develop SnackBar for both Android and iOS devices. So in this tutorial we would going to create a material design custom SnackBar with animation for both Android and iOS mobile phone applications.
Special Thanks to my friend Pankaj Sood, Who actually make this SnackBar.
Contents in this project Create Material Design SnackBar in Android iOS Example :
1. Start a fresh React Native project. If you don’t know how then read my this tutorial.
2. Import AppRegistry, Animated, View, Platform, Button, Image, StyleSheet and Text component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { AppRegistry, Animated, View, Platform, Button, Image, StyleSheet, Text } from ‘react-native’;
|
3. Create a New class in your project named as MaterialDesignSnackBar .
We are using the React Native’s own Animated component library in our tutorial to show and hide the SnackBar. You don’t need to do anything in this code , I have already made the best for you Guys , Enjoy .
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
class MaterialDesignSnackBar extends Component
{
constructor()
{
super();
this.animatedValue = new Animated.Value(60);
this.ShowSnackBar = false;
this.HideSnackBar = true;
this.state = { SnackBarInsideMsgHolder: ” };
}
ShowSnackBarFunction(SnackBarInsideMsgHolder=“Default SnackBar Message…”, duration=3000)
{
if( this.ShowSnackBar === false )
{
this.setState({ SnackBarInsideMsgHolder: SnackBarInsideMsgHolder });
this.ShowSnackBar = true;
Animated.timing
(
this.animatedValue,
{
toValue: 0,
duration: 350
}
).start(this.hide(duration));
}
}
hide = (duration) =>
{
this.timerID = setTimeout(() =>
{
if(this.HideSnackBar === true)
{
this.HideSnackBar = false;
Animated.timing
(
this.animatedValue,
{
toValue: 60,
duration: 350
}
).start(() =>
{
this.HideSnackBar = true;
this.ShowSnackBar = false;
clearTimeout(this.timerID);
})
}
}, duration);
}
SnackBarCloseFunction = () =>
{
if(this.HideSnackBar === true)
{
this.HideSnackBar = false;
clearTimeout(this.timerID);
Animated.timing
(
this.animatedValue,
{
toValue: 60,
duration: 350
}
).start(() =>
{
this.ShowSnackBar = false;
this.HideSnackBar = true;
});
}
}
render()
{
return(
<Animated.View style = {[{ transform: [{ translateY: this.animatedValue }]}, styles.SnackBarContainterView ]}>
<Text numberOfLines = { 1 } style = { styles.SnackBarInsideTextStyle }>{ this.state.SnackBarInsideMsgHolder }</Text>
<Text style = { styles.SnackBarUndoTextStyle} onPress = { this.SnackBarCloseFunction } > UNDO </Text>
</Animated.View>
);
}
}
|
4. Create constructor in your own project’s class with super method.
1
2
3
4
|
constructor()
{
super();
}
|
5. Create a function in your class named as DisplayMaterialDesignSnackBar .
In this function you have to pass your own message text which would automatically set inside the SnackBar.
1
2
3
4
|
DisplayMaterialDesignSnackBar = () =>
{
this.refs.ReactNativeCodeSnackBar.ShowSnackBarFunction(“ReactNativeCode.com”);
}
|
6. Crate a Parent View in render’s return block.
1
2
3
4
5
6
7
8
9
10
|
render()
{
return(
<View style = { styles.MainContainer }>
</View>
);
}
|
7. Add a Button component inside the Parent View and Call the DisplayMaterialDesignSnackBar function on onPress event of button.
1
2
3
4
5
6
7
8
9
10
11
|
render()
{
return(
<View style = { styles.MainContainer }>
<Button onPress = { this.DisplayMaterialDesignSnackBar } title=” Show Material Design SnackBar “ > </Button>
</View>
);
}
|
8. Create a object of your MaterialDesignSnackBar class like component declaration after Button component and pass the ref = “ReactNativeCodeSnackBar” . This would allow us to show the SnackBar in our application.
1
2
3
4
5
6
7
8
9
10
11
12
|
render()
{
return(
<View style = { styles.MainContainer }>
<Button onPress = { this.DisplayMaterialDesignSnackBar } title=” Show Material Design SnackBar “ > </Button>
<MaterialDesignSnackBar ref = “ReactNativeCodeSnackBar”/>
</View>
);
}
|
9. Create CSS style sheet classes .
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
|
const styles = StyleSheet.create(
{
MainContainer:
{
flex: 1,
justifyContent: ‘center’,
paddingHorizontal: 20,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0
},
SnackBarContainterView:
{
position: ‘absolute’,
backgroundColor: ‘#009688’,
flexDirection: ‘row’,
alignItems: ‘center’,
left: 0,
bottom: 0,
right: 0,
height: 60,
paddingLeft: 10,
paddingRight: 55
},
SnackBarInsideTextStyle:
{
color: ‘#fff’,
fontSize: 18
},
SnackBarUndoTextStyle:{
color: ‘#FFEB3B’,
fontSize: 18,
position: ‘absolute’,
right: 10,
justifyContent: ‘center’,
padding: 5
}
});
|
10. Complete source code for index.android.js / index.ios.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import React, { Component } from ‘react’;
import { AppRegistry, Animated, View, Platform, Button, Image, StyleSheet, Text } from ‘react-native’;
class MaterialDesignSnackBar extends Component
{
constructor()
{
super();
this.animatedValue = new Animated.Value(60);
this.ShowSnackBar = false;
this.HideSnackBar = true;
this.state = { SnackBarInsideMsgHolder: ” };
}
ShowSnackBarFunction(SnackBarInsideMsgHolder=“Default SnackBar Message…”, duration=3000)
{
if( this.ShowSnackBar === false )
{
this.setState({ SnackBarInsideMsgHolder: SnackBarInsideMsgHolder });
this.ShowSnackBar = true;
Animated.timing
(
this.animatedValue,
{
toValue: 0,
duration: 350
}
).start(this.hide(duration));
}
}
hide = (duration) =>
{
this.timerID = setTimeout(() =>
{
if(this.HideSnackBar === true)
{
this.HideSnackBar = false;
Animated.timing
(
this.animatedValue,
{
toValue: 60,
duration: 350
}
).start(() =>
{
this.HideSnackBar = true;
this.ShowSnackBar = false;
clearTimeout(this.timerID);
})
}
}, duration);
}
SnackBarCloseFunction = () =>
{
if(this.HideSnackBar === true)
{
this.HideSnackBar = false;
clearTimeout(this.timerID);
Animated.timing
(
this.animatedValue,
{
toValue: 60,
duration: 350
}
).start(() =>
{
this.ShowSnackBar = false;
this.HideSnackBar = true;
});
}
}
render()
{
return(
<Animated.View style = {[{ transform: [{ translateY: this.animatedValue }]}, styles.SnackBarContainterView ]}>
<Text numberOfLines = { 1 } style = { styles.SnackBarInsideTextStyle }>{ this.state.SnackBarInsideMsgHolder }</Text>
<Text style = { styles.SnackBarUndoTextStyle} onPress = { this.SnackBarCloseFunction } > UNDO </Text>
</Animated.View>
);
}
}
class Project extends Component
{
constructor()
{
super();
}
DisplayMaterialDesignSnackBar = () =>
{
this.refs.ReactNativeCodeSnackBar.ShowSnackBarFunction(“ReactNativeCode.com”);
}
render()
{
return(
<View style = { styles.MainContainer }>
<Button onPress = { this.DisplayMaterialDesignSnackBar } title=” Show Material Design SnackBar “ > </Button>
<MaterialDesignSnackBar ref = “ReactNativeCodeSnackBar”/>
</View>
);
}
}
const styles = StyleSheet.create(
{
MainContainer:
{
flex: 1,
justifyContent: ‘center’,
paddingHorizontal: 20,
paddingTop: (Platform.OS) === ‘ios’ ? 20 : 0
},
SnackBarContainterView:
{
position: ‘absolute’,
backgroundColor: ‘#009688’,
flexDirection: ‘row’,
alignItems: ‘center’,
left: 0,
bottom: 0,
right: 0,
height: 60,
paddingLeft: 10,
paddingRight: 55
},
SnackBarInsideTextStyle:
{
color: ‘#fff’,
fontSize: 18
},
SnackBarUndoTextStyle:{
color: ‘#FFEB3B’,
fontSize: 18,
position: ‘absolute’,
right: 10,
justifyContent: ‘center’,
padding: 5
}
});
AppRegistry.registerComponent(‘Project’, () => Project);
|
Screenshot of Application in iOS mobile phone device :
Screenshot of Application in Android mobile phone device :